8
votes

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.

4
aside from your question AVAudioSessionCategoryPlayAndRecord will lower the output volume. if you're not doing VOIP (or any other record and play at same time) avoid setting your category to this.Nicolas Manzini
Discovered a bit more information. I was setting up my AVAudioSession before configuring my OpenAL. By setting the AVAudioSession info afterward (all in the viewDidLoad, only now at the end), the audio now plays to the bluetooth speakers. Now, I am not able to record however. I am finding there may be a conflict between output to bluetooth and input from built-in mic. Any suggestions?TheTwoNotes
Hello! If you are still on SO would you mind taking a look at a similar question of mine? It my question from 4/9/2018. Thanks!!Nerdy Bunz

4 Answers

2
votes

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.

2
votes

this is the solution at the moment, but is deprecated and anyone know the new solution, but add this part of code on your app now and all working very well!!

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

Hope this help you!

1
votes

Not possible.

From the very interesting Apple doc AVAudioSession -- Selecting a microphone QA1799:

If an application uses the setPreferredInput:error: method to select a Bluetooth HFP input, the output will automatically be changed to the Bluetooth HFP output. Moreover, selecting a Bluetooth HFP output using the MPVolumeView's route picker will automatically change the input to the Bluetooth HFP input. Therefore both the input and output will always end up on the Bluetooth HFP device even though only the input or output was set individually.

1
votes

Did you check with setCategory withOptions? Its start from iOS 6

    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionAllowBluetooth error:&error];