Search code examples
swiftavaudiosession

What is the proper usage of the "notifyOthersOnDeactivation" option for AVAudioSession's setActive function?


I am using this sample code for reference to add speechRecognizer to my app.

The following function…

private static func prepareEngine() throws -> (AVAudioEngine, SFSpeechAudioBufferRecognitionRequest) {

contains the line:

try audioSession.setActive(true, options: .notifyOthersOnDeactivation)

But Apple's documentation for notifyOthersOnDeactivation states:

Only use this option when deactivating your audio session; that is, when you pass a value of false to the setActive(_:options:) instance method.

I want to get clarity if this is some undocumented way of deactivating audioSession (I doubt that), or if in general, app that activates audioSession should always deactivated if at some point before or during becoming inactive, background or terminated.


Solution

  • This code is contrary to documentation and does not make a lot of sense in how it configures the session. However, it's copied directly from Apple's sample code. I can't say why Apple is publishing code that directly contradicts their documentation. The use of .duckOthers with .record also violates the docs. I would open a Feedback asking them to fix the docs or the sample code.