Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by voporak5 · Mar 19, 2019 at 03:23 PM · iosbluetooth

Trouble getting good bluetooth audio behavior ios

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)

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

137 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Input.GetKey with iOS Bluetooth Keyboard 1 Answer

How to get BlueTooth Keyboard Value in the IOS APPS? 0 Answers

Why will audio files loaded in runtime not play through bluetooth on iOS devices. 0 Answers

How to connect two IOS devices with bluetooth 0 Answers

Heart rate Bluetooth PLUGIN IOS 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges