I get weird behavior with my application, but only when connected to a bluetooth speaker. When you usually use your phone, it behaves as expected.
applications:
- - a one-page application with one ViewController that never changes
- has buttons that activate short temporary sounds that are played:
- AVAudioPlayer (just playing files)
- AVAudioPlayerNode (using scheduled buffers and scheduled files)
- AVSpeechSynthesizer (utterance statements)
deals with AVAudioSession like this (in AppDelegate):
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { // Override point for customization after application launch. let audioSession = AVAudioSession.sharedInstance() do { try audioSession.setCategory(AVAudioSessionCategoryPlayback, with: [.allowBluetoothA2DP]) } catch { print("Setting category to AVAudioSessionCategoryPlayback failed.") } // Other project setup return true }
PS - I really donβt know why it contains an additional [.allowBluetoothA2DP] option, but I think I was just trying to figure out if this helped ... it is not.
So the oddity is this:
Let's say I run my application in the foreground and it makes a sound and everything is fine ... and Spotify is paused in the background.
I go to Spotify and click play ... and the song is playing ... excellent ...
But then I return to my application ...
Now my application has no sound for an arbitrary period of time, until it somehow decides to get the sound again. (5-15 seconds?)
Again, this only happens on bluetooth.
So, the rule of thumb is as follows:
If my application is connected to the background and another application requests Core Audio for exceptional sound resolution ... but then I return to my application ... although the other application is paused again, the "audio permission" is not successfully "transferred" to my application, but only via bluetooth!
source share