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 phoneWithout 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 AVAudioSessionCategoryOptionAllowBluetoothA2DPStill trying to figure out why all of the audio from the app gets shut down when playing Apple Music (MPMusicPlayerController)
Post
Replies
Boosts
Views
Activity
Is anyone else seeing the Apple Music API acting sporadically?
The API call we are using is
https://api.music.apple.com/v1/catalog/{0}/search?offset={1}&term={2}&limit=20&types=songs
If we do
https://api.music.apple.com/v1/catalog/US/search?offset=0&term=maroon+5&limit=20&types=songs
and
https://api.music.apple.com/v1/catalog/US/search?offset=20&term=maroon+5&limit=20&types=songs
the returned results from the api call that has an offset of 20 are the same results as the returned api call that had an offset of 0.
We've also seen it where instead of searching maroon+5 we search lady+gaga and that doesn't work either however it is currently working whereas we've searched lionel+richie and that did work but currently is not working thus producing the same behavior as maroon+5.
After changing the limit to 10 to test out that behavior, instead of returning songs 11-20 on the second api call with an offset of 10, (https://api.music.apple.com/v1/catalog/US/search?offset=10&term=maroon+5&limit=10&types=songs) songs 1-10 were returned again where as doing
https://api.music.apple.com/v1/catalog/US/search?offset=0&term=maroon+5&limit=20&types=songs returned songs 1-20