Dive into the technical aspects of audio on your device, including codecs, format support, and customization options.

Audio Documentation

Posts under Audio subtopic

Post

Replies

Boosts

Views

Activity

Problems with AVAudioPlayerNode's scheduleBuffer function
Hi,I'm having two problems using the scheduleBuffer function of AVAudioPlayerNode.Background: my app generates audio programatically, which is why I am using this function. I also need low latency. Therefore, I'm using a strategy of scheduling a small number of buffers, and using the completion handler to keep the process moving forward by scheduling one more buffer for each one that completes.I'm seeing two problems with this approach:One, the total memory consumed by my app grows steadily while the audio is playing, which suggests that the audio buffers are never being deallocated or some other runaway process is underway. (The Leaks tool doesn't detect any leaks, however).Two, audio playback sometimes stops, particularly on slower devices. By "stops", what I mean is that at some point I schedule a buffer and the completion block for that buffer is never called. When this happens, I can't even clear the problem by stopping the player.Now, regarding the first issue, I suspected that if my completion block recursively scheduled another buffer with another completion block, I would probably end up blowing out the stack with an infinite recursion. To get around this, instead of directly scheduling the buffer in the completion block, I set it up to enqueue the schedule in a dispatch queue. However, this doesn't seem to solve the problem.Any advice would be appreciated. Thanks.
9
0
4.9k
Oct ’23
How to resume audio playing of other apps after dismissing AVPlayerViewController?
I use AVPlayerViewController to play short videos in my app.If there is an app playing audio in background before user plays a video in my app, I want the background audio playing of the other app to resume after my video player is dismissed. I currently use AVAudioSession.setActive(false, with: .notifyOthersOnDeactivation) to do that.Even though Apple's Music app and Podcasts app do resume playing after I call AVAudioSession.setActive(false, with: .notifyOthersOnDeactivation)—won't resume without the call so it means this call does have effect—none of the 3rd party music or podcast apps that I tested(Spotify, SoundCloud, Amazon Music, Overcast) do.I doubt it's because none of these popular 3rd party apps supports background audio resuming. There must be something missing in my code:class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() } override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) // I know it's not the best place to call setActive nor it covers all the cases. It's just a convenient place to put the code to test its effect after dismissing the video player. do { try AVAudioSession.sharedInstance().setActive(false, with: .notifyOthersOnDeactivation) try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient) try AVAudioSession.sharedInstance().setActive(true) } catch { print(error) } } @IBAction func play(_ sender: Any) { do { try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback) try AVAudioSession.sharedInstance().setActive(true) } catch { print(error) } let playerController = AVPlayerViewController() playerController.player = AVPlayer(url: URL(string: "http://gslb.miaopai.com/stream/UkjiD45ddxZFQ79I2bLaGg__.mp4")!) playerController.player?.play() present(playerController, animated: true) } }Even though someone on Stack Overflow thinks this code is fine and its these 3rd party apps' fault, I still believe there are something that can be done to make them resume audio playback because Twitter app can. But I don't know what Twitter does to achieve that, anyone knows?PS: here is the complete project so anyone interested can try it.
1
0
2.7k
Feb ’22
CoreAudio crash - AVAudioIONodeImpl.mm:365: _GetHWFormat: required condition is false: hwFormat
Hi All,I'm working with two application modules:1) Recording module with this audioSession setup:try audioSession.setCategory(AVAudioSessionCategoryRecord) try audioSession.setMode(AVAudioSessionModeMeasurement) try audioSession.setPreferredIOBufferDuration(0.05) try self.audioSession.setActive(true)2) Recording module with this audioSession setup: try audioSession.setCategory(AVAudioSessionCategoryPlayback) try audioSession.setMode(AVAudioSessionModeDefault) try self.audioSession.setActive(true)For each passage from 1->2 and 2-1 I have atry self.audioSession.setActive(false)If I pass from 1) module to 2) or redo 1) all works fine. Than if from 2) I come to 1) I get this error ontry self.audioSession.setActive(true)This is the error: ERROR: [0x16e10b000] >avae> AVAudioIONodeImpl.mm:365: _GetHWFormat: required condition is false: hwFormatWhat is this error related to? I can't find any help on Apple iOS documentation to understand where the problem can be.Does anybody have any tip?
18
0
7.1k
Mar ’23
AVAudioBuffer and μLaw
I'd like to use AVAudioConverter to convert audio captured from the microphone to μLaw.Unfortunately, when I try to create an output buffer to convert into, I get an exception. I've tried both AVAudioPCMBuffer and AVAudioCompressedBuffer, and neither works for me.Is this supposed to work?Thanks!let format = AVAudioFormat(settings: [AVFormatIDKey: NSNumber(value: kAudioFormatULaw), AVSampleRateKey: 8000, AVNumberOfChannelsKey: 1]) let buffer = AVAudioPCMBuffer(pcmFormat: format, frameCapacity: 1000) // required condition is false: isPCMFormat let buffer = AVAudioCompressedBuffer(format: format, packetCapacity: 1000) // required condition is false: !(fmt.IsPCM() || fmt.mFormatID == kAudioFormatALaw || fmt.mFormatID == kAudioFormatULaw)
2
0
1.7k
Aug ’21
Audio Balance Setting
HiI am writing an app for people with variable hearing issues - I need to be able to change the the left-right volume of music being played (Apple Music, Spotify, etc) dynamically as the environment changes.Being able to set the Accessibility options (Settings > General > Accessibility - Left-Right Audio Balance) from code would be perfect.Does anyone know how I can programmatically set and adjust this left-right volume balance from my Swift app?I've Googled this to death and can't find anything.Andy
2
0
1.4k
Nov ’21
is it possible to turn on the torch while having the front camera open (swift/objective c)
I am using the UIImagePickerController with sourceType = .camera and cameraDevice = .frontI would like the user to be able to toggle the torch mode with this camera open. However, when the torch turns on, the camera is disabled (blurred out).Is there a work around to accomplish this.Any help/suggestions are greatly accepted. Thanks
1
0
1k
Apr ’23
video playback error in airplay mode
I am developing a video playback application , which can play ads along with the content.I am successfully able to start the playback with ad on device (ipad/iphone) but it never playsback in airplay mode. I gets the the below error notification: "AVPlayerItemFailedToPlayToEndTimeErrorKey = "Error Domain=AVFoundationErrorDomain Code=-11800 \"The operation could not be completed\" UserInfo={NSUnderlyingError=0x17005eff0 {Error Domain=NSOSStatusErrorDomain Code=-12926 \"(null)\"}, NSLocalizedFailureReason=An unknown error occurred (-12926), NSLocalizedDescription=The operation could not be completed" Any idea what could be wrong ?
3
0
2.1k
Sep ’21
Audio Unit v3 Instrument with inputBusses problem
Hello,I am having problems getting my AUv3 Instrument with an inputBus to work. As a standalone app (with the SimplePlayEngine of the sample code integrated) it seems to work fine, the plugin also passes the auval test without errors. But when I try to use the plugin in a host application (like garageband / logic / host of the sample code) I can't get any output, the internalRenderBlock is not even being called. I narrowed it down to the inputBusses property, so it seems that I am doing something wrong with setting up the input bus.To reproduce, take the InstrumentDemo of the Apple sample code, and in the init method initialize an inputBusBuffer, create an inputBusArray with the bus of the inputBusBuffer. Set the inputBusArray as the return value for the inputBusses property and allocateRenderResources of the inputBusBuffer in the allocateRenderResourcesAndReturnError (and deallocateRenderResources in the deallocateRenderResources call). All of this is done analogous to the inputBus setup in the FilterDemo example.I also explicitly set the channelCapabilities to Stereo In, Stereo Out.Omitting the further processing in the internalRenderBlock, shouldn't this work to the point that internalRenderBlock is getting called? Ít is getting called in the App, and auval validation succeeds, but it is not being called in any host.Am I missing something here?Any help will be much appreciated!
4
0
2.4k
May ’23
dismiss AVPlayer after pressing done or video is finished
Hi,i am trying to play different local videos on a button press inside a collection view cell.My problem:I start the first video by pressing the button and presenting the AVPlayerplayerViewController.player?.playI can now rotate my device and the video is playing in landscape mode.After I press done it goes back to the collectionview and I can choose the second video.Now the second video plays, but when i go into landscape mode the first Video shows upand there is no done or back button.If I stay in portrait mode everything is fine..Any ideas?Thanks a lot.
1
0
1k
Feb ’22
Getting error SetOutputFormat: required condition is false: format.sampleRate == hwFormat.sampleRate not found any reference stucked in the app
import UIKitimport Speechclass ViewController: UIViewController, SFSpeechRecognizerDelegate ,AVSpeechSynthesizerDelegate { @IBOutlet weak var textView: UITextView! private let speechRecognizer = SFSpeechRecognizer(locale: Locale.init(identifier: "en-US"))! private var recognitionRequest: SFSpeechAudioBufferRecognitionRequest? private var recognitionTask: SFSpeechRecognitionTask? private let audioEngine = AVAudioEngine() private var audio : AVAudioInputNode? = nil var audioSession : AVAudioSession? = nil override func viewDidLoad() { super.viewDidLoad() self.getSpeech(asSpeach: "how are you ?") speechRecognizer.delegate = self SFSpeechRecognizer.requestAuthorization { (authStatus) in var isButtonEnabled = false switch authStatus { case .authorized: isButtonEnabled = true case .denied: isButtonEnabled = false print("User denied access to speech recognition") case .restricted: isButtonEnabled = false print("Speech recognition restricted on this device") case .notDetermined: isButtonEnabled = false print("Speech recognition not yet authorized") } } } func endAudio() { if audioEngine.isRunning { AudioOutputUnitStop((audioEngine.inputNode?.audioUnit)!) AudioUnitUninitialize((audioEngine.inputNode?.audioUnit)!) audioEngine.stop() } } func startRecording() { if recognitionTask != nil { recognitionTask?.cancel() recognitionTask = nil } AudioOutputUnitStop((audioEngine.inputNode?.audioUnit)!) AudioUnitUninitialize((audioEngine.inputNode?.audioUnit)!) audioSession = AVAudioSession.sharedInstance() do { try audioSession?.setCategory(AVAudioSessionCategoryPlayAndRecord) try audioSession?.setMode(AVAudioSessionModeMeasurement) try audioSession?.setActive(true, with: .notifyOthersOnDeactivation) } catch { print("audioSession properties weren't set because of an error.") } recognitionRequest = SFSpeechAudioBufferRecognitionRequest() guard let inputNode = audioEngine.inputNode else { print("Audio engine has no input node") } audio=inputNode audio?.removeTap(onBus: 2) guard let recognitionRequest = recognitionRequest else { fatalError("Unable to create an SFSpeechAudioBufferRecognitionRequest object") } recognitionRequest.shouldReportPartialResults = true recognitionTask = speechRecognizer.recognitionTask(with: recognitionRequest, resultHandler: { (result, error) in var isFinal = false if result != nil { self.textView.text = result?.bestTranscription.formattedString isFinal = (result?.isFinal)! if self.textView.text == "Fine" || self.textView.text == "Find" { self.audioEngine.stop() self.textView.text = "" DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) { self.getSpeech(asSpeach: "how are you ?") } } } if error != nil || isFinal { self.audioEngine.stop() inputNode.removeTap(onBus: 2) self.recognitionRequest = nil self.recognitionTask = nil } }) let recordingFormat = inputNode.outputFormat(forBus: 2) inputNode.installTap(onBus: 2, bufferSize: 1024, format: recordingFormat) { (buffer, when) in self.recognitionRequest?.append(buffer) } audioEngine.prepare() do { try audioEngine.start() } catch { print("audioEngine couldn't start because of an error.") } textView.text = "Say something, I'm listening!" } func speechRecognizer(_ speechRecognizer: SFSpeechRecognizer, availabilityDidChange available: Bool) { if available { //something } else { //else thing } } func getSpeech(asSpeach:String) { audioSession = AVAudioSession.sharedInstance() do { try audioSession?.overrideOutputAudioPort(.speaker) } catch { print(error.localizedDescription) } let synthesizer = AVSpeechSynthesizer() let utterance = AVSpeechUtterance(string: asSpeach) utterance.rate = AVSpeechUtteranceDefaultSpeechRate synthesizer.delegate=self synthesizer.speak(utterance) } public func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, didFinish utterance: AVSpeechUtterance) { audioEngine.stop() do { try audioSession?.setActive(false) } catch { print("error.localizedDescription") print(error.localizedDescription) } print("speech synthesizer ended") if(audioEngine.isRunning) { endAudio() } DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { self.startRecording() } }}As requirement first dictet one string and if the string is a keyword (as per business rule) dictet again but getting crash think the crash is for mismatching the audioformat for dictet and speech .The error message isERROR: [0x1b2a4cb40] >avae> AVAudioIONodeImpl.mm:884: SetOutputFormat: required condition is false: format.sampleRate == hwFormat.sampleRateTerminating app due to uncaught exception 'com.apple.coreaudio.avfaudio', reason: 'required condition is false: format.sampleRate == hwFormat.sampleRate'Think the problem is with AVAudioSession please suggest i am trying many times but dont have a luck till now
6
0
8k
Jul ’22
Disable "Hold & Accept" CallKit option
Is it possible to disable (or not show) the "Hold & Accept" option for an incoming second call with CallKit? I have a VoIP app in which some calls cannot be held. I would like to either not give the user the Hold & Accept option OR be able to somehow display to them that this will terminate the first call (or decline the second call depending upon the final implementation).
2
0
3.5k
Aug ’22
BTLE CoreMIDI: automatically reconnect
My app successfully receives midi from BT MIDI devices when they are enabled through the CABTMidiLocalPeripheralViewController pattern. However, I would like the app to automatically reconnect to these BT MIDI devices in subsequent sessions whenever they are available. Is this possible without requiring the user to go through the connection view controller every time?
1
0
1.3k
Sep ’21
AVAudioSession resume
I'm using AVAudioSession in my VOIP app (using also CallKit).I understand that the session can get interrupted by a number of things, for example by a second incoming call.Apple states towards the bottom of this pagehttps://developer.apple.com/library/content/documentation/Audio/Conceptual/AudioSessionProgrammingGuide/HandlingAudioInterruptions/HandlingAudioInterruptions.html#//apple_ref/doc/uid/TP40007875-CH4-SW5"Note: There is no guarantee that a begin interruption will have a corresponding end interruption. Your app needs to be aware of a switch to a foreground running state or the user pressing a Play button. In either case, determine whether your app should reactivate its audio session."On the mentioned page there is an example, when the user ignores the incoming call and in that case AVAudioSessionInterruptionTypeEnded is being sent.But what should I do in every other case, when I don't get an AVAudioSessionInterruptionTypeEnded? (E.g. When the user answers the 2nd call and puts me on hold and later he ends the 2nd call?)Thanks!
5
0
3.5k
Nov ’22
Streaming from external camera
Hi,I'm researching possible ways to stream external video and audio into an iOS device, up until now I came up with the the following "solutions" (more like hacks) -Developing MFi CameraI couldn't find any existing external camera that works using MFi so I guess it's reasonable that Apple are not allowing MFi cameras, will appreciate official say on the manner.Wifi CameraThere are several problems with this - Connectivity-wise - The iOS device needs to be on the same network as the wifi camera, if the camera provides its own network internet connectivity won't work (Wifi + Celluar can't work together right?)LatencyMultipeerConnectivity - This is not really a solution since it allows connecting to other iOS devices, right?Lightning to USB 3 Adapter - this allows connecting a PTP/mass storage device only to iPads afaikWill appreciate any other ideas on how to connect to an external camera stream!
2
0
5.2k
Jan ’22
Record video with audio via Bluetooth
I'm trying to record video, with audio input coming from a connected Bluetooth headset. However I haven't been able to get an AVCaptureDevice that represents the headset, so I can't add it to my AVCaptureSession.I tried to find it using a discovery session, but it never finds the headset-- only the built in microphone. That may not be surprising since I'm asking for .builtInMicrophone, but the enumeration has no members for external audio inputs. I tried leaving the device type array empty but that finds no devices.let discoverySession = AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInMicrophone], mediaType: AVMediaType.audio, position: .unspecified) print("Found \(discoverySession.devices.count) devices") for device in discoverySession.devices { print("Device: \(device)") }After doing some searching I tried setting up the AVAudioSession to specifically allow Bluetooth. However this has had no effect on the above.private let session = AVCaptureSession() // later... session.usesApplicationAudioSession = true session.automaticallyConfiguresApplicationAudioSession = false do { try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayAndRecord, with: [.allowBluetooth]) try AVAudioSession.sharedInstance().setActive(true) } catch { print("Error messing with audio session: \(error)") }For completeness I also tried the deprecated AVCaptureDevice.devices() method, but it doesn't find the Bluetooth headset either.I know that the headset is available because AVAudioSession can see it. However I haven't been able to get from the availableInputs array to something I can use in an AVCaptureSession. The following code finds the headset, but what would I do with the result? It's not an AVCaptureDevice nor can I construct one from the entries in the array. Setting the "preferred" input doesn't have any effect that I know how to use.if let availableInputs = AVAudioSession.sharedInstance().availableInputs { print("Found \(availableInputs.count) inputs") for input in availableInputs { print("Input: \(input)") if input.portType == AVAudioSessionPortBluetoothHFP { print("Setting preferred input") do { try AVAudioSession.sharedInstance().setPreferredInput(input) } catch { print("Error setting preferred input: \(error)") } } } }Given that the Bluetooth headset is connected and available, how do I set it as the audio input for my capture session?
3
0
5.2k
May ’22
When isCameraIntrinsicMatrixDeliverySupported?
I'm trying to obtain the intrinsic matrix for each video frame of AVCaptureSession (the same intrisic matrix as ARKit provides), however the isCameraIntrinsicMatrixDeliverySupported property of AVCaptureConnection is false in my use-case.The documentation of the property says "This property's value is true only if both the connection's input device format and output class support delivery of camera intrinsics."How do I know which device formats support delivery of intrinsic matrix? What do I need to do to be able to enable the intrinsic matrix delivery?Simple code to illustrate my problem:import UIKit import AVFoundation class ViewController: UIViewController { var sess: AVCaptureSession! var sessOut: AVCaptureVideoDataOutput! var prevLayer: AVCaptureVideoPreviewLayer! override func viewDidLoad() { super.viewDidLoad() sess = AVCaptureSession() let device = AVCaptureDevice.default(.builtInWideAngleCamera, for: AVMediaType.video, position: .back) let input = try! AVCaptureDeviceInput(device: device!) sess.addInput(input) sessOut = AVCaptureVideoDataOutput() sess.addOutput(sessOut) sessOut.connections.first?.videoOrientation = .landscapeRight sessOut.connections.first?.preferredVideoStabilizationMode = .cinematic print(sessOut.connections.first?.isCameraIntrinsicMatrixDeliverySupported) // <-- false - why? prevLayer = AVCaptureVideoPreviewLayer(session: sess) prevLayer.frame = self.view.frame prevLayer.videoGravity = .resizeAspectFill prevLayer.connection?.videoOrientation = .landscapeRight self.view.layer.addSublayer(prevLayer) sess.startRunning() } }
5
0
2.5k
Mar ’22
CoreMediaErrorDomain Error -12312
I have been seeing a weird error as shown below in the screenshot. The weirder thing is that the video I was trying to stream plays fine most of the time but only failed a few times due to this error. And from the error message I still couldn't figure out what went wrong with the stream. In the app I am simply using AVPlayer to stream HLS and nothing fancy. Any AVFundation or HLS experts here could shed some lights on this? Much appreciated!Edit: Not sure why attached image is not showing. The error message says:"The operation couldn't be completed. (CoreMediaErrorDomain error -12312 - Media Entry URL not match previous playlist for MEDIA-SEQUENCE 0:http://the-stream-segment1.ts vs http://the-stream-segment2.ts)
1
0
4.9k
Sep ’22
Camera preview becomes slow - AVFoundation
Device: iPad Pro 10.5-inchIOS: 10.3.2 10.3.3I use the following function to take a snapshot in the app.UIView (UISnapshotting) - (BOOL) drawViewHierarchyInRect: (CGRect) rect afterScreenUpdates: (BOOL) afterUpdates;CGRect rect = self.view.bounds; UIGraphicsBeginImageContextWithOptions(rect.size, NO, [[UIScreen mainScreen] scale]); [self.view drawViewHierarchyInRect:rect afterScreenUpdates:YES]; UIImage *snapshot = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext();However, every time I use this feature, the Camera preview rendering by AVFoundation used in another part of the application became slow. self.session = [[AVCaptureSession alloc] init]; AVCaptureDevice *camera = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; self.input = [[AVCaptureDeviceInput alloc] initWithDevice:camera error:&error]; [self.session addInput:self.input]; self.output = [[AVCapturePhotoOutput alloc] init]; [self.session addOutput:self.output]; self.prevLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.session]; self.prevLayer.frame = self.previewView.layer.bounds; self.prevLayer.videoGravity = AVLayerVideoGravityResizeAspectFill; switch ([[UIApplication sharedApplication] statusBarOrientation]) { case UIInterfaceOrientationLandscapeLeft:{ AVCaptureConnection *con = self.prevLayer.connection; con.videoOrientation = AVCaptureVideoOrientationLandscapeLeft; } break; case UIInterfaceOrientationLandscapeRight:{ AVCaptureConnection *con = self.prevLayer.connection; con.videoOrientation = AVCaptureVideoOrientationLandscapeRight; } break; case UIInterfaceOrientationPortrait:{ AVCaptureConnection *con = self.prevLayer.connection; con.videoOrientation = AVCaptureVideoOrientationPortrait; } break; case UIInterfaceOrientationPortraitUpsideDown:{ AVCaptureConnection *con = self.prevLayer.connection; con.videoOrientation = AVCaptureVideoOrientationPortraitUpsideDown; } break; default: break; } [self.previewView.layer addSublayer:self.prevLayer]; [self.session startRunning];Currently we are seeing this problem only in the above device and OS environment.Anyone can solve this? I need information that will lead to resolution.
1
0
1.5k
Sep ’21
Fetching ID3 metadata while HLS stream is being played on AppleTV via Airplay
I am working on AVPlayer based streaming apps wherein I am relying on ID3 tags for fetching metadata from the HLS stream. I am using KVO for this and observing AVPlayerItem’s timedMetadata property. This works perfectly well in normal scenario. However when I route the playback to Apple TV via Airplay, I do not get any notifications for the said KVO. In fact, the timedMetadata property resets to nil once the Airplay is activated as the API docs says here,“As an optimization for playback, AVPlayerItem may omit the processing of timed metadata when no observer of this property is registered. Therefore, when no such observer is registered, the value of the timedMetadata property may remain nil regardless of the contents of the underlying media.”Is there any way, we can fetch timedMetadata property of AVPlayerItem while the steam is being played via Airplay mode.
2
0
2.1k
Jun ’23
Burst Mode Using Bracketed Capture?
Eventhough some apps out there figured out how to do proper burst capture that is equal to stock iOS Camera App, there is no official API to do so.Currently I have it where it wil call capturePhoto over and over, which is not fast enough. I've seen some hints floating around that the proper way to do this is using bracketed capture any input on this?Thanks in advance.
1
1
3.6k
Apr ’23
Problems with AVAudioPlayerNode's scheduleBuffer function
Hi,I'm having two problems using the scheduleBuffer function of AVAudioPlayerNode.Background: my app generates audio programatically, which is why I am using this function. I also need low latency. Therefore, I'm using a strategy of scheduling a small number of buffers, and using the completion handler to keep the process moving forward by scheduling one more buffer for each one that completes.I'm seeing two problems with this approach:One, the total memory consumed by my app grows steadily while the audio is playing, which suggests that the audio buffers are never being deallocated or some other runaway process is underway. (The Leaks tool doesn't detect any leaks, however).Two, audio playback sometimes stops, particularly on slower devices. By "stops", what I mean is that at some point I schedule a buffer and the completion block for that buffer is never called. When this happens, I can't even clear the problem by stopping the player.Now, regarding the first issue, I suspected that if my completion block recursively scheduled another buffer with another completion block, I would probably end up blowing out the stack with an infinite recursion. To get around this, instead of directly scheduling the buffer in the completion block, I set it up to enqueue the schedule in a dispatch queue. However, this doesn't seem to solve the problem.Any advice would be appreciated. Thanks.
Replies
9
Boosts
0
Views
4.9k
Activity
Oct ’23
How to resume audio playing of other apps after dismissing AVPlayerViewController?
I use AVPlayerViewController to play short videos in my app.If there is an app playing audio in background before user plays a video in my app, I want the background audio playing of the other app to resume after my video player is dismissed. I currently use AVAudioSession.setActive(false, with: .notifyOthersOnDeactivation) to do that.Even though Apple's Music app and Podcasts app do resume playing after I call AVAudioSession.setActive(false, with: .notifyOthersOnDeactivation)—won't resume without the call so it means this call does have effect—none of the 3rd party music or podcast apps that I tested(Spotify, SoundCloud, Amazon Music, Overcast) do.I doubt it's because none of these popular 3rd party apps supports background audio resuming. There must be something missing in my code:class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() } override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) // I know it's not the best place to call setActive nor it covers all the cases. It's just a convenient place to put the code to test its effect after dismissing the video player. do { try AVAudioSession.sharedInstance().setActive(false, with: .notifyOthersOnDeactivation) try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient) try AVAudioSession.sharedInstance().setActive(true) } catch { print(error) } } @IBAction func play(_ sender: Any) { do { try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback) try AVAudioSession.sharedInstance().setActive(true) } catch { print(error) } let playerController = AVPlayerViewController() playerController.player = AVPlayer(url: URL(string: "http://gslb.miaopai.com/stream/UkjiD45ddxZFQ79I2bLaGg__.mp4")!) playerController.player?.play() present(playerController, animated: true) } }Even though someone on Stack Overflow thinks this code is fine and its these 3rd party apps' fault, I still believe there are something that can be done to make them resume audio playback because Twitter app can. But I don't know what Twitter does to achieve that, anyone knows?PS: here is the complete project so anyone interested can try it.
Replies
1
Boosts
0
Views
2.7k
Activity
Feb ’22
CoreAudio crash - AVAudioIONodeImpl.mm:365: _GetHWFormat: required condition is false: hwFormat
Hi All,I'm working with two application modules:1) Recording module with this audioSession setup:try audioSession.setCategory(AVAudioSessionCategoryRecord) try audioSession.setMode(AVAudioSessionModeMeasurement) try audioSession.setPreferredIOBufferDuration(0.05) try self.audioSession.setActive(true)2) Recording module with this audioSession setup: try audioSession.setCategory(AVAudioSessionCategoryPlayback) try audioSession.setMode(AVAudioSessionModeDefault) try self.audioSession.setActive(true)For each passage from 1->2 and 2-1 I have atry self.audioSession.setActive(false)If I pass from 1) module to 2) or redo 1) all works fine. Than if from 2) I come to 1) I get this error ontry self.audioSession.setActive(true)This is the error: ERROR: [0x16e10b000] >avae> AVAudioIONodeImpl.mm:365: _GetHWFormat: required condition is false: hwFormatWhat is this error related to? I can't find any help on Apple iOS documentation to understand where the problem can be.Does anybody have any tip?
Replies
18
Boosts
0
Views
7.1k
Activity
Mar ’23
AVAudioBuffer and μLaw
I'd like to use AVAudioConverter to convert audio captured from the microphone to μLaw.Unfortunately, when I try to create an output buffer to convert into, I get an exception. I've tried both AVAudioPCMBuffer and AVAudioCompressedBuffer, and neither works for me.Is this supposed to work?Thanks!let format = AVAudioFormat(settings: [AVFormatIDKey: NSNumber(value: kAudioFormatULaw), AVSampleRateKey: 8000, AVNumberOfChannelsKey: 1]) let buffer = AVAudioPCMBuffer(pcmFormat: format, frameCapacity: 1000) // required condition is false: isPCMFormat let buffer = AVAudioCompressedBuffer(format: format, packetCapacity: 1000) // required condition is false: !(fmt.IsPCM() || fmt.mFormatID == kAudioFormatALaw || fmt.mFormatID == kAudioFormatULaw)
Replies
2
Boosts
0
Views
1.7k
Activity
Aug ’21
Audio Balance Setting
HiI am writing an app for people with variable hearing issues - I need to be able to change the the left-right volume of music being played (Apple Music, Spotify, etc) dynamically as the environment changes.Being able to set the Accessibility options (Settings > General > Accessibility - Left-Right Audio Balance) from code would be perfect.Does anyone know how I can programmatically set and adjust this left-right volume balance from my Swift app?I've Googled this to death and can't find anything.Andy
Replies
2
Boosts
0
Views
1.4k
Activity
Nov ’21
is it possible to turn on the torch while having the front camera open (swift/objective c)
I am using the UIImagePickerController with sourceType = .camera and cameraDevice = .frontI would like the user to be able to toggle the torch mode with this camera open. However, when the torch turns on, the camera is disabled (blurred out).Is there a work around to accomplish this.Any help/suggestions are greatly accepted. Thanks
Replies
1
Boosts
0
Views
1k
Activity
Apr ’23
video playback error in airplay mode
I am developing a video playback application , which can play ads along with the content.I am successfully able to start the playback with ad on device (ipad/iphone) but it never playsback in airplay mode. I gets the the below error notification: "AVPlayerItemFailedToPlayToEndTimeErrorKey = "Error Domain=AVFoundationErrorDomain Code=-11800 \"The operation could not be completed\" UserInfo={NSUnderlyingError=0x17005eff0 {Error Domain=NSOSStatusErrorDomain Code=-12926 \"(null)\"}, NSLocalizedFailureReason=An unknown error occurred (-12926), NSLocalizedDescription=The operation could not be completed" Any idea what could be wrong ?
Replies
3
Boosts
0
Views
2.1k
Activity
Sep ’21
Audio Unit v3 Instrument with inputBusses problem
Hello,I am having problems getting my AUv3 Instrument with an inputBus to work. As a standalone app (with the SimplePlayEngine of the sample code integrated) it seems to work fine, the plugin also passes the auval test without errors. But when I try to use the plugin in a host application (like garageband / logic / host of the sample code) I can't get any output, the internalRenderBlock is not even being called. I narrowed it down to the inputBusses property, so it seems that I am doing something wrong with setting up the input bus.To reproduce, take the InstrumentDemo of the Apple sample code, and in the init method initialize an inputBusBuffer, create an inputBusArray with the bus of the inputBusBuffer. Set the inputBusArray as the return value for the inputBusses property and allocateRenderResources of the inputBusBuffer in the allocateRenderResourcesAndReturnError (and deallocateRenderResources in the deallocateRenderResources call). All of this is done analogous to the inputBus setup in the FilterDemo example.I also explicitly set the channelCapabilities to Stereo In, Stereo Out.Omitting the further processing in the internalRenderBlock, shouldn't this work to the point that internalRenderBlock is getting called? Ít is getting called in the App, and auval validation succeeds, but it is not being called in any host.Am I missing something here?Any help will be much appreciated!
Replies
4
Boosts
0
Views
2.4k
Activity
May ’23
dismiss AVPlayer after pressing done or video is finished
Hi,i am trying to play different local videos on a button press inside a collection view cell.My problem:I start the first video by pressing the button and presenting the AVPlayerplayerViewController.player?.playI can now rotate my device and the video is playing in landscape mode.After I press done it goes back to the collectionview and I can choose the second video.Now the second video plays, but when i go into landscape mode the first Video shows upand there is no done or back button.If I stay in portrait mode everything is fine..Any ideas?Thanks a lot.
Replies
1
Boosts
0
Views
1k
Activity
Feb ’22
Getting error SetOutputFormat: required condition is false: format.sampleRate == hwFormat.sampleRate not found any reference stucked in the app
import UIKitimport Speechclass ViewController: UIViewController, SFSpeechRecognizerDelegate ,AVSpeechSynthesizerDelegate { @IBOutlet weak var textView: UITextView! private let speechRecognizer = SFSpeechRecognizer(locale: Locale.init(identifier: "en-US"))! private var recognitionRequest: SFSpeechAudioBufferRecognitionRequest? private var recognitionTask: SFSpeechRecognitionTask? private let audioEngine = AVAudioEngine() private var audio : AVAudioInputNode? = nil var audioSession : AVAudioSession? = nil override func viewDidLoad() { super.viewDidLoad() self.getSpeech(asSpeach: "how are you ?") speechRecognizer.delegate = self SFSpeechRecognizer.requestAuthorization { (authStatus) in var isButtonEnabled = false switch authStatus { case .authorized: isButtonEnabled = true case .denied: isButtonEnabled = false print("User denied access to speech recognition") case .restricted: isButtonEnabled = false print("Speech recognition restricted on this device") case .notDetermined: isButtonEnabled = false print("Speech recognition not yet authorized") } } } func endAudio() { if audioEngine.isRunning { AudioOutputUnitStop((audioEngine.inputNode?.audioUnit)!) AudioUnitUninitialize((audioEngine.inputNode?.audioUnit)!) audioEngine.stop() } } func startRecording() { if recognitionTask != nil { recognitionTask?.cancel() recognitionTask = nil } AudioOutputUnitStop((audioEngine.inputNode?.audioUnit)!) AudioUnitUninitialize((audioEngine.inputNode?.audioUnit)!) audioSession = AVAudioSession.sharedInstance() do { try audioSession?.setCategory(AVAudioSessionCategoryPlayAndRecord) try audioSession?.setMode(AVAudioSessionModeMeasurement) try audioSession?.setActive(true, with: .notifyOthersOnDeactivation) } catch { print("audioSession properties weren't set because of an error.") } recognitionRequest = SFSpeechAudioBufferRecognitionRequest() guard let inputNode = audioEngine.inputNode else { print("Audio engine has no input node") } audio=inputNode audio?.removeTap(onBus: 2) guard let recognitionRequest = recognitionRequest else { fatalError("Unable to create an SFSpeechAudioBufferRecognitionRequest object") } recognitionRequest.shouldReportPartialResults = true recognitionTask = speechRecognizer.recognitionTask(with: recognitionRequest, resultHandler: { (result, error) in var isFinal = false if result != nil { self.textView.text = result?.bestTranscription.formattedString isFinal = (result?.isFinal)! if self.textView.text == "Fine" || self.textView.text == "Find" { self.audioEngine.stop() self.textView.text = "" DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) { self.getSpeech(asSpeach: "how are you ?") } } } if error != nil || isFinal { self.audioEngine.stop() inputNode.removeTap(onBus: 2) self.recognitionRequest = nil self.recognitionTask = nil } }) let recordingFormat = inputNode.outputFormat(forBus: 2) inputNode.installTap(onBus: 2, bufferSize: 1024, format: recordingFormat) { (buffer, when) in self.recognitionRequest?.append(buffer) } audioEngine.prepare() do { try audioEngine.start() } catch { print("audioEngine couldn't start because of an error.") } textView.text = "Say something, I'm listening!" } func speechRecognizer(_ speechRecognizer: SFSpeechRecognizer, availabilityDidChange available: Bool) { if available { //something } else { //else thing } } func getSpeech(asSpeach:String) { audioSession = AVAudioSession.sharedInstance() do { try audioSession?.overrideOutputAudioPort(.speaker) } catch { print(error.localizedDescription) } let synthesizer = AVSpeechSynthesizer() let utterance = AVSpeechUtterance(string: asSpeach) utterance.rate = AVSpeechUtteranceDefaultSpeechRate synthesizer.delegate=self synthesizer.speak(utterance) } public func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, didFinish utterance: AVSpeechUtterance) { audioEngine.stop() do { try audioSession?.setActive(false) } catch { print("error.localizedDescription") print(error.localizedDescription) } print("speech synthesizer ended") if(audioEngine.isRunning) { endAudio() } DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { self.startRecording() } }}As requirement first dictet one string and if the string is a keyword (as per business rule) dictet again but getting crash think the crash is for mismatching the audioformat for dictet and speech .The error message isERROR: [0x1b2a4cb40] >avae> AVAudioIONodeImpl.mm:884: SetOutputFormat: required condition is false: format.sampleRate == hwFormat.sampleRateTerminating app due to uncaught exception 'com.apple.coreaudio.avfaudio', reason: 'required condition is false: format.sampleRate == hwFormat.sampleRate'Think the problem is with AVAudioSession please suggest i am trying many times but dont have a luck till now
Replies
6
Boosts
0
Views
8k
Activity
Jul ’22
Disable "Hold & Accept" CallKit option
Is it possible to disable (or not show) the "Hold & Accept" option for an incoming second call with CallKit? I have a VoIP app in which some calls cannot be held. I would like to either not give the user the Hold & Accept option OR be able to somehow display to them that this will terminate the first call (or decline the second call depending upon the final implementation).
Replies
2
Boosts
0
Views
3.5k
Activity
Aug ’22
BTLE CoreMIDI: automatically reconnect
My app successfully receives midi from BT MIDI devices when they are enabled through the CABTMidiLocalPeripheralViewController pattern. However, I would like the app to automatically reconnect to these BT MIDI devices in subsequent sessions whenever they are available. Is this possible without requiring the user to go through the connection view controller every time?
Replies
1
Boosts
0
Views
1.3k
Activity
Sep ’21
AVAudioSession resume
I'm using AVAudioSession in my VOIP app (using also CallKit).I understand that the session can get interrupted by a number of things, for example by a second incoming call.Apple states towards the bottom of this pagehttps://developer.apple.com/library/content/documentation/Audio/Conceptual/AudioSessionProgrammingGuide/HandlingAudioInterruptions/HandlingAudioInterruptions.html#//apple_ref/doc/uid/TP40007875-CH4-SW5"Note: There is no guarantee that a begin interruption will have a corresponding end interruption. Your app needs to be aware of a switch to a foreground running state or the user pressing a Play button. In either case, determine whether your app should reactivate its audio session."On the mentioned page there is an example, when the user ignores the incoming call and in that case AVAudioSessionInterruptionTypeEnded is being sent.But what should I do in every other case, when I don't get an AVAudioSessionInterruptionTypeEnded? (E.g. When the user answers the 2nd call and puts me on hold and later he ends the 2nd call?)Thanks!
Replies
5
Boosts
0
Views
3.5k
Activity
Nov ’22
Streaming from external camera
Hi,I'm researching possible ways to stream external video and audio into an iOS device, up until now I came up with the the following "solutions" (more like hacks) -Developing MFi CameraI couldn't find any existing external camera that works using MFi so I guess it's reasonable that Apple are not allowing MFi cameras, will appreciate official say on the manner.Wifi CameraThere are several problems with this - Connectivity-wise - The iOS device needs to be on the same network as the wifi camera, if the camera provides its own network internet connectivity won't work (Wifi + Celluar can't work together right?)LatencyMultipeerConnectivity - This is not really a solution since it allows connecting to other iOS devices, right?Lightning to USB 3 Adapter - this allows connecting a PTP/mass storage device only to iPads afaikWill appreciate any other ideas on how to connect to an external camera stream!
Replies
2
Boosts
0
Views
5.2k
Activity
Jan ’22
Record video with audio via Bluetooth
I'm trying to record video, with audio input coming from a connected Bluetooth headset. However I haven't been able to get an AVCaptureDevice that represents the headset, so I can't add it to my AVCaptureSession.I tried to find it using a discovery session, but it never finds the headset-- only the built in microphone. That may not be surprising since I'm asking for .builtInMicrophone, but the enumeration has no members for external audio inputs. I tried leaving the device type array empty but that finds no devices.let discoverySession = AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInMicrophone], mediaType: AVMediaType.audio, position: .unspecified) print("Found \(discoverySession.devices.count) devices") for device in discoverySession.devices { print("Device: \(device)") }After doing some searching I tried setting up the AVAudioSession to specifically allow Bluetooth. However this has had no effect on the above.private let session = AVCaptureSession() // later... session.usesApplicationAudioSession = true session.automaticallyConfiguresApplicationAudioSession = false do { try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayAndRecord, with: [.allowBluetooth]) try AVAudioSession.sharedInstance().setActive(true) } catch { print("Error messing with audio session: \(error)") }For completeness I also tried the deprecated AVCaptureDevice.devices() method, but it doesn't find the Bluetooth headset either.I know that the headset is available because AVAudioSession can see it. However I haven't been able to get from the availableInputs array to something I can use in an AVCaptureSession. The following code finds the headset, but what would I do with the result? It's not an AVCaptureDevice nor can I construct one from the entries in the array. Setting the "preferred" input doesn't have any effect that I know how to use.if let availableInputs = AVAudioSession.sharedInstance().availableInputs { print("Found \(availableInputs.count) inputs") for input in availableInputs { print("Input: \(input)") if input.portType == AVAudioSessionPortBluetoothHFP { print("Setting preferred input") do { try AVAudioSession.sharedInstance().setPreferredInput(input) } catch { print("Error setting preferred input: \(error)") } } } }Given that the Bluetooth headset is connected and available, how do I set it as the audio input for my capture session?
Replies
3
Boosts
0
Views
5.2k
Activity
May ’22
When isCameraIntrinsicMatrixDeliverySupported?
I'm trying to obtain the intrinsic matrix for each video frame of AVCaptureSession (the same intrisic matrix as ARKit provides), however the isCameraIntrinsicMatrixDeliverySupported property of AVCaptureConnection is false in my use-case.The documentation of the property says "This property's value is true only if both the connection's input device format and output class support delivery of camera intrinsics."How do I know which device formats support delivery of intrinsic matrix? What do I need to do to be able to enable the intrinsic matrix delivery?Simple code to illustrate my problem:import UIKit import AVFoundation class ViewController: UIViewController { var sess: AVCaptureSession! var sessOut: AVCaptureVideoDataOutput! var prevLayer: AVCaptureVideoPreviewLayer! override func viewDidLoad() { super.viewDidLoad() sess = AVCaptureSession() let device = AVCaptureDevice.default(.builtInWideAngleCamera, for: AVMediaType.video, position: .back) let input = try! AVCaptureDeviceInput(device: device!) sess.addInput(input) sessOut = AVCaptureVideoDataOutput() sess.addOutput(sessOut) sessOut.connections.first?.videoOrientation = .landscapeRight sessOut.connections.first?.preferredVideoStabilizationMode = .cinematic print(sessOut.connections.first?.isCameraIntrinsicMatrixDeliverySupported) // <-- false - why? prevLayer = AVCaptureVideoPreviewLayer(session: sess) prevLayer.frame = self.view.frame prevLayer.videoGravity = .resizeAspectFill prevLayer.connection?.videoOrientation = .landscapeRight self.view.layer.addSublayer(prevLayer) sess.startRunning() } }
Replies
5
Boosts
0
Views
2.5k
Activity
Mar ’22
CoreMediaErrorDomain Error -12312
I have been seeing a weird error as shown below in the screenshot. The weirder thing is that the video I was trying to stream plays fine most of the time but only failed a few times due to this error. And from the error message I still couldn't figure out what went wrong with the stream. In the app I am simply using AVPlayer to stream HLS and nothing fancy. Any AVFundation or HLS experts here could shed some lights on this? Much appreciated!Edit: Not sure why attached image is not showing. The error message says:"The operation couldn't be completed. (CoreMediaErrorDomain error -12312 - Media Entry URL not match previous playlist for MEDIA-SEQUENCE 0:http://the-stream-segment1.ts vs http://the-stream-segment2.ts)
Replies
1
Boosts
0
Views
4.9k
Activity
Sep ’22
Camera preview becomes slow - AVFoundation
Device: iPad Pro 10.5-inchIOS: 10.3.2 10.3.3I use the following function to take a snapshot in the app.UIView (UISnapshotting) - (BOOL) drawViewHierarchyInRect: (CGRect) rect afterScreenUpdates: (BOOL) afterUpdates;CGRect rect = self.view.bounds; UIGraphicsBeginImageContextWithOptions(rect.size, NO, [[UIScreen mainScreen] scale]); [self.view drawViewHierarchyInRect:rect afterScreenUpdates:YES]; UIImage *snapshot = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext();However, every time I use this feature, the Camera preview rendering by AVFoundation used in another part of the application became slow. self.session = [[AVCaptureSession alloc] init]; AVCaptureDevice *camera = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; self.input = [[AVCaptureDeviceInput alloc] initWithDevice:camera error:&error]; [self.session addInput:self.input]; self.output = [[AVCapturePhotoOutput alloc] init]; [self.session addOutput:self.output]; self.prevLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.session]; self.prevLayer.frame = self.previewView.layer.bounds; self.prevLayer.videoGravity = AVLayerVideoGravityResizeAspectFill; switch ([[UIApplication sharedApplication] statusBarOrientation]) { case UIInterfaceOrientationLandscapeLeft:{ AVCaptureConnection *con = self.prevLayer.connection; con.videoOrientation = AVCaptureVideoOrientationLandscapeLeft; } break; case UIInterfaceOrientationLandscapeRight:{ AVCaptureConnection *con = self.prevLayer.connection; con.videoOrientation = AVCaptureVideoOrientationLandscapeRight; } break; case UIInterfaceOrientationPortrait:{ AVCaptureConnection *con = self.prevLayer.connection; con.videoOrientation = AVCaptureVideoOrientationPortrait; } break; case UIInterfaceOrientationPortraitUpsideDown:{ AVCaptureConnection *con = self.prevLayer.connection; con.videoOrientation = AVCaptureVideoOrientationPortraitUpsideDown; } break; default: break; } [self.previewView.layer addSublayer:self.prevLayer]; [self.session startRunning];Currently we are seeing this problem only in the above device and OS environment.Anyone can solve this? I need information that will lead to resolution.
Replies
1
Boosts
0
Views
1.5k
Activity
Sep ’21
Fetching ID3 metadata while HLS stream is being played on AppleTV via Airplay
I am working on AVPlayer based streaming apps wherein I am relying on ID3 tags for fetching metadata from the HLS stream. I am using KVO for this and observing AVPlayerItem’s timedMetadata property. This works perfectly well in normal scenario. However when I route the playback to Apple TV via Airplay, I do not get any notifications for the said KVO. In fact, the timedMetadata property resets to nil once the Airplay is activated as the API docs says here,“As an optimization for playback, AVPlayerItem may omit the processing of timed metadata when no observer of this property is registered. Therefore, when no such observer is registered, the value of the timedMetadata property may remain nil regardless of the contents of the underlying media.”Is there any way, we can fetch timedMetadata property of AVPlayerItem while the steam is being played via Airplay mode.
Replies
2
Boosts
0
Views
2.1k
Activity
Jun ’23
Burst Mode Using Bracketed Capture?
Eventhough some apps out there figured out how to do proper burst capture that is equal to stock iOS Camera App, there is no official API to do so.Currently I have it where it wil call capturePhoto over and over, which is not fast enough. I've seen some hints floating around that the proper way to do this is using bracketed capture any input on this?Thanks in advance.
Replies
1
Boosts
1
Views
3.6k
Activity
Apr ’23