Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AVAudioPlayer and AVAudioSession won't play to BlueTooth stereo on iPad (2 or otherwise)

I have an app that plays recorded audio as well as repeating sounds. The sounds play correctly through the onboard iPad speaker, and if I plug in a cord from the earphone jack to my stereo audio-in, it also plays well. When I pair my iPad to my bluetooth stereo input, all the sounds from my other apps (written for iPhone, running on my iPad) work fine, as does all other sound from my device.

The problem is my app written for iPad is NOT playing over the bluetooth path, but instead plays from the built-in speakers.

In my app delegate in the didFinishLaunchingWithOptions(…) method, I have placed 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 being called and there are no errors being returned.

In my controller code, I have 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 are playing short .01 second sounds repeated in a threaded controlled loop and I do this using OpenAL :

    alSourcePlay(sourceID);

This is the same code as I have in my other apps written for iPhone that works as desired.

I realize that there are other threads regarding bluetooth input, but I am having a specific issue with bluetooth output of audio sounds from my iPad app.

like image 682
TheTwoNotes Avatar asked Jan 23 '14 16:01

TheTwoNotes


People also ask

Can you connect 2 Bluetooth speakers at the same time iPad?

However, to make things simpler, Apple with iOS 13.2 update has added a new feature called Share Audio and it allows users to listen to the same audio on two different Bluetooth devices at the same time.

Why is my Bluetooth device connected but not playing?

It is possible that your device appears to be correctly paired, but yet it has an error. A faulty or weak Bluetooth network often causes this. To resolve this, you will have to unpair the device first. Restart the speaker, then pair the speaker with the Bluetooth device again.


1 Answers

Because your category is Play and Record, you would have to enable Bluetooth as an input in order for it to be supported as an output (by default, the same receiver is used for input/output in Play and Record mode). In order to do that, you would have to set an extra property on your AVAudioSession:

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

You will also want to check that you didn't force the output to the built in speaker anywhere in your code, by setting the kAudioSessionProperty_OverrideCategoryDefaultToSpeaker property on your session.

like image 196
JP Hribovsek Avatar answered Sep 23 '22 12:09

JP Hribovsek