Trouble getting good bluetooth audio behavior

We are trying to get good bluetooth behavior for our app but there are a whole slew of problems.


There are 2 issues that I don't understand how to resolve,


1. I don't receive notifications from AVAudioSessionRouteChangeNotification if user starts app without Bluetooth headphones connected.


2. If I stream with Apple Music, after adding the code to enable Bluetooth, all audio from my app is lost unless I put app in background then bring it to the foreground at which the audio is now forced into the earpiece speaker phone


Without making any changes to the code to try and incorporate bluetooth capabilities our app currently forces audio through the speakers.


To enable bluetooth we've added the following code just before the app finishes initializing which works great if the user already has their bluetooth headphones connected. The audio will successfully be outputed into the headphones. If they disconnect their headphones the audio will be outputted to the device speakers.


[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord 
    withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker | AVAudioSessionCategoryOptionAllowBluetoothA2DP | AVAudioSessionCategoryOptionAllowAirPlay 
                                 error:nil];
    
[[AVAudioSession sharedInstance] setActive:YES error:nil];


However if the user is streaming music through our app then behavior becomes very poor. We have 2 ways of streaming music, the first is using an AVPlayer to stream music from a URL, the other way is using MPMusicPlayerController to stream music from Apple Music.


After adding the code to enable bluetooth wireless headphones, if the user starts streaming music with Apple Music, all audio such as sound effects from our app get turned off and the only thing the user can hear is the music being streamed. If they stop streaming the music the audio never comes back. If they then turn off the bluetooth wireless headphones and put the app into the background followed by bringing it back into the foreground, the sound effects come back but now they are outputted through the earphone speaker.


That is the worst of all the behavior we see.


When streaming with the AVPlayer we don't see any issues with the app sound effects being lost but, if the user disconnects their bluetooth wireless headphones while streaming I cannot get the app to detect when they become available again if the user turns them on. I have the following code to listen when the audio routes change and that works great when disconnecting the bluetooth wireless headphones, but when reconnecting them I can't get a notification unless after I've turned my bluetooth headphones on that I put the app into the background and bring it back into the foreground.


NSNotificationCenter* notificationCenter = [NSNotificationCenter defaultCenter];
    
[notificationCenter addObserver:media_sharedInstance selector:@selector(audioHardwareRouteChanged:) name:AVAudioSessionRouteChangeNotification object:nil];


- (void) audioHardwareRouteChanged:(NSNotification *)notification{
    NSInteger routeChangeReason = [notification.userInfo[AVAudioSessionRouteChangeReasonKey] integerValue];
    
    NSLog(@"route change");
    NSLog(@"%ld",(long)routeChangeReason);
    
    NSLog(@"output volume %f",[[AVAudioSession sharedInstance] outputVolume]);
    
    if(routeChangeReason == AVAudioSessionRouteChangeReasonOldDeviceUnavailable)
    {
        //[media_sharedInstance RemoveAudioChange];
        NSLog(@"old device unavailable");
        [media_sharedInstance ConfigureSpeakers];
        //[media_sharedInstance SubscribeToAudioChange];
    }
    if(routeChangeReason == kAudioSessionRouteChangeReason_NewDeviceAvailable)
    {
        NSLog(@"new device available");
        //[media_sharedInstance RemoveAudioChange];
        [media_sharedInstance ConfigureSpeakers];
        //[media_sharedInstance SubscribeToAudioChange];
    }


}

- (void) ConfigureSpeakers
{
    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord
                                     withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker | AVAudioSessionCategoryOptionAllowBluetooth | AVAudioSessionCategoryOptionAllowAirPlay
                                           error:nil];
    
    [[AVAudioSession sharedInstance] setActive:YES error:nil];
}


Edit* I figured out the issue with AVAudioSessionRouteChangeNotification not sending a notification. The solution was to use AVAudioSessionCategoryOptionAllowBluetoothA2DP and AVAudioSessionCategoryOptionAllowBluetooth when I received the notification that the bluetooth was disconnected, and when I get the notification that bluetooth becomes available I switch the options to just AVAudioSessionCategoryOptionAllowBluetoothA2DP


Still trying to figure out why all of the audio from the app gets shut down when playing Apple Music (MPMusicPlayerController)

Replies

Hello! Any chance you found a solution when playing Apple Music alongside an AVAudioPlayer?