AVAudioPlayer and AVAudioSession will not play on BlueTooth stereo on iPad (2 or otherwise)

I have an application that plays back recorded sound, as well as repeating sounds. Sounds are played correctly through the built-in iPad speaker, and if I connect the cord from the headphone jack to the stereo input, it also plays well. When I connect my iPad to my bluetooth stereo input, all sounds from my other applications (written for iPhone, running on my iPad) work just fine, like all other sounds from my device.

The problem is that my application written for the iPad does not play along the Bluetooth path, but instead plays from the built-in speakers.

In my applet in the didFinishLaunchingWithOptions (...) method, I posted the following:

NSError *error = nil; [[AVAudioSession sharedInstance] setMode:AVAudioSessionModeDefault error:&error]; [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:&error]; [[AVAudioSession sharedInstance] setActive:YES error:&error]; 

This code is called and no errors are returned.

In my controller code, I recorded samples that I play using AVAudioPlayer as follows:

 audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:recordURL error:&error]; audioPlayer.numberOfLoops = 0; [audioPlayer setDelegate:self]; [audioPlayer play]; 

In other areas, I have drones that play short sounds .01 seconds, repeating in a stream controlled circuit, and I do this with OpenAL:

  alSourcePlay(sourceID); 

This is the same code that I have in other applications written for the iPhone, which works as desired.

I understand that there are other topics related to the bluetooth input, but I have a certain problem with the output of the Bluetooth audio signals from my iPad application.

+8
ios objective-c ipad avaudioplayer avaudiosession
source share
4 answers

Impossible.

From a very interesting Apple AVAudioSession document - QA1799 Microphone Selection:

If the application uses the setPreferredInput: error: method to select the Bluetooth HFP input, the output will automatically be changed to the Bluetooth HFP output. Moreover, selecting the HFP Bluetooth output using the MPVolumeView route selector will automatically change the input signal to the HFP Bluetooth input. Therefore, the input and output will always be on the Bluetooth HFP device, even if only the input or output has been set separately.

+1
source share

Since your category is β€œPlay and Record,” you need to enable Bluetooth as an input so that it is supported as an output (by default, the same receiver is used for input / output in playback and recording mode). To do this, you will need to set an additional property on your AVAudioSession:

 UInt32 allowBluetoothInput = 1; AudioSessionSetProperty ( kAudioSessionProperty_OverrideCategoryEnableBluetoothInput, sizeof (allowBluetoothInput), &allowBluetoothInput ); 

You will also want to verify that you did not put output on the built-in speaker anywhere in your code by setting the kAudioSessionProperty_OverrideCategoryDefaultToSpeaker property to the session.

+2
source share

This solution is currently, but it is deprecated and everyone knows the new solution, but add this part of the code to your application now and everything works very well.

  UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback; AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(sessionCategory), &sessionCategory); [[UIApplication sharedApplication] beginReceivingRemoteControlEvents]; 

Hope this helps you!

+2
source share

Have you checked with setCategory withOptions? Its beginning with iOS 6

  [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionAllowBluetooth error:&error]; 
+1
source share

All Articles