how to route iPhone audio to the bluetooth headset - iphone

I'm attempting to output audio to the bluetooth headset (not A2DP) using AVAudioPlayer, AVAudioSession and AudioSessionSetProperty.
There seems to be functions to select the bluetooth headset as input (kAudioSessionProperty_OverrideCategoryEnableBluetoothInput), but no equivalent for setting the output. This is done in the Voicemail app, where you can select the headset, handset speaker or speaker phone. I've tried various combinations of SessionCategories and the AudioSession properties, but I just can't seem to hit on an approach that works.
I'm sure someone has figured this out, care to share an example?

This little test worked for me... it involves setting up the bluetooth headset as the input also (not sure if that's what you want). Sorry about the crappy formatting on the code...
// create and set up the audio session
AVAudioSession* audioSession = [AVAudioSession sharedInstance];
[audioSession setDelegate:self];
[audioSession setCategory: AVAudioSessionCategoryPlayAndRecord error: nil];
[audioSession setActive: YES error: nil];
// set up for bluetooth microphone input
UInt32 allowBluetoothInput = 1;
OSStatus stat = AudioSessionSetProperty (
kAudioSessionProperty_OverrideCategoryEnableBluetoothInput,
sizeof (allowBluetoothInput),
&allowBluetoothInput
);
NSLog(#"status = %x", stat); // problem if this is not zero
// check the audio route
UInt32 size = sizeof(CFStringRef);
CFStringRef route;
OSStatus result = AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &size, &route);
NSLog(#"route = %#", route);
// if bluetooth headset connected, should be "HeadsetBT"
// if not connected, will be "ReceiverAndMicrophone"
// now, play a quick sound we put in the bundle (bomb.wav)
CFBundleRef mainBundle = CFBundleGetMainBundle();
CFURLRef soundFileURLRef;
SystemSoundID soundFileObject;
soundFileURLRef = CFBundleCopyResourceURL (mainBundle,CFSTR ("bomb"),CFSTR ("wav"),NULL);
AudioServicesCreateSystemSoundID (soundFileURLRef,&soundFileObject);
AudioServicesPlaySystemSound (soundFileObject); // should play into headset
Hope that helps!

I was able to get this working, but it took some doing. I've pieced together the pertinent piece of code here:
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:&err];
UInt32 allowBluetoothInput = 1;
AudioSessionSetProperty(
kAudioSessionProperty_OverrideCategoryEnableBluetoothInput,
sizeof (allowBluetoothInput),
&allowBluetoothInput);
It is also possible to which sources are available and switch between bluetooth, a headset, handset or the speakerphone but things become very involved at that point. The audio source manager I ultimately wrote was over 700 lines.

Related

Audio played using AVAudioPlayer is working in iPhone Simulator but not in the Device

I need to play a video along with a small 2 sec audio play. Everything works fine in iPhone simulator but not in the Device. I am storing my audio file in the project bundle and playing the audio from there.
NSString *gotItsoundFilePath = [[NSBundle mainBundle] pathForResource:#"gotit" ofType:#"mp3"];
NSData *gotItsampleData = [[NSData alloc] initWithContentsOfFile:gotItsoundFilePath];
NSError *gotItaudioError = nil;
gotItAudioPlayer = [[AVAudioPlayer alloc] initWithData:gotItsampleData error:&gotItaudioError];
gotItAudioPlayer.numberOfLoops = 0;
This code works fine in iPhone Simulator when i use [gotItAudioPlayer play]; but the same build is not working in the device. Am i doing wrong somewhere? Can someone please help on this?
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayAndRecord error: nil];
UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride);
Put this code. before you make player object.

Routing audio input to receive from TOP microphone on iPhone

I am writing a little app to record multiple tracks and play them back over one another.
I am using the PlaybackAndRecord mode and i am routing my output to the main speakers.
Problem is that the bottom microphone is still being used for input as well so now I when I record I get the output from the other tracks really loud on the new track.
Here is what I have so far:
audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
OSStatus propertySetError = 0;
UInt32 allowMixing = true;
propertySetError = AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof(allowMixing), &allowMixing);
UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride);
[audioSession setActive:YES error:nil];
Use AVAudioSession to get available inputs. On my iPhone 5 it looks like this.
NSLog(#"%#", [AVAudioSession sharedInstance].availableInputs);
"<AVAudioSessionPortDescription: 0x14554400, type = MicrophoneBuiltIn; name = iPhone Microphone; UID = Built-In Microphone; selectedDataSource = Back>"
Then use one of these inputs to get availableDataSources, like this.
NSLog(#"%#", [AVAudioSession sharedInstance].availableInputs[0].dataSources);
"<AVAudioSessionDataSourceDescription: 0x145afb00, ID = 1835216945; name = Bottom>",
"<AVAudioSessionDataSourceDescription: 0x145b1870, ID = 1835216946; name = Front>",
"<AVAudioSessionDataSourceDescription: 0x145b3650, ID = 1835216947; name = Back>"
We can see that there are actually THREE microphones on this iPhone: top front, top back, and bottom. Now you can set your preferred data source.
AVAudioSessionPortDescription *port = [AVAudioSession sharedInstance].availableInputs[0];
for (AVAudioSessionDataSourceDescription *source in port.dataSources) {
if ([source.dataSourceName isEqualToString:#"Back"]) {
[port setPreferredDataSource:source error:nil];
}
}

System Sound Services Won't play audio when connected to a car's bluetooth

I am using System Sound Services to play audio in my application
It works perfectly when using speakers, headphones or even a dock USB car stereo connection.
However when the phone is connected to a car bluetooth system the audio won't play.
I imagine that since it's connected to a bluetooth device the System Sound Services would try to send to the audio through Bluetooth.
Does anyone knows how I can prevent the audio route from trying to send to the sound through Bluetooth?
Here's the code I am using:
CFURLRef soundFileURLRef;
SystemSoundID soundFileObject;
CFBundleRef mainBundle;
mainBundle = CFBundleGetMainBundle ();
// Get the URL to the sound file to play
soundFileURLRef = CFBundleCopyResourceURL (
mainBundle,
CFSTR ("Alert6"),
CFSTR ("aif"),
NULL
);
// Create a system sound object representing the sound file
AudioServicesCreateSystemSoundID (
soundFileURLRef,
&soundFileObject
);
AudioServicesPlaySystemSound (soundFileObject);
Any ideas?
You can override the output to the internal speaker
UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker; // lets the system sounds come out of the speaker
OSStatus err = AudioSessionSetProperty(kAudioSessionProperty_OverrideAudioRoute, sizeof(audioRouteOverride), &audioRouteOverride);
if (err != noErr) {
// handle error
}

why the volume of sounds does not change in my app?

I used code below in my application, and if I increase or decrease the volume from the iPhone's button it's still the same, but if the phone is in silent mode the voice will be muted.
The code:
-(void) playNote: (NSString *)Note type: (NSString *)type
{
CFURLRef soundFileURLRef;
SystemSoundID soundFileObject;
CFBundleRef mainBundle;
mainBundle = CFBundleGetMainBundle ();
// Get the URL to the sound file to play
soundFileURLRef = CFBundleCopyResourceURL (mainBundle,
(CFStringRef)Note,
(CFStringRef)type,
NULL);
NSLog(#"%#\n", soundFileURLRef);
// Create a system sound object representing the sound file
AudioServicesCreateSystemSoundID (soundFileURLRef, &soundFileObject);
AudioServicesPlaySystemSound (soundFileObject);
}
What am I doing wrong?
You need to set your Audio Category properly before playing back sounds.
UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
AudioSessionSetProperty (kAudioSessionProperty_AudioCategory, sizeof (sessionCategory), &sessionCategory);

iphone, how to play sound even in silent or mute mode?

as topic... is it possible ?
Thanks
again, I have attached the code as follows, please check which step is wrong .thanks.
//#step
AudioSessionInitialize (NULL, NULL, NULL, NULL);
AudioSessionSetActive(true);
UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
OSStatus error = AudioSessionSetProperty (kAudioSessionProperty_AudioCategory, sizeof(sessionCategory),&sessionCategory);
if (error)
printf("ERROR AudioSessionSetProperty ! %d\n", error);
//#step
NSString* filePath = #"AlarmClockBell.caf";
[Util restoreResourceFile:filePath];
filePath =[Util getFileFullPathFromSysDoc:filePath];
NSURL *soundFileURL = [NSURL fileURLWithPath:filePath];
NSError* error ;
AVAudioPlayer * audioPalyer = [[AVAudioPlayer alloc] initWithContentsOfURL: soundFileURL error: &error];
if (nil == audioPalyer)
{
AppTrace3(self, #"Faild to play", soundFileURL, error);
return FALSE;
}
[audioPalyer prepareToPlay];
[audioPalyer setVolume: 5 ];
[audioPalyer setDelegate: self];
audioPalyer.numberOfLoops = 10;
[audioPalyer play];
thanks...
If you look in the docs under Audio Session Categories, you'll find a number of modes that you can set to tell the system how your app plans to use audio. The default is AVAudioSessionCategorySoloAmbient which tracks the ring/silent switch and the screen lock.
To have your app ignore the ring/silent switch settings, you could try changing the category:
#import <AudioToolbox/AudioToolbox.h>
AudioSessionInitialize (NULL, NULL, NULL, NULL);
AudioSessionSetActive(true);
UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
AudioSessionSetProperty (kAudioSessionProperty_AudioCategory,
sizeof(sessionCategory),&sessionCategory);
If you want to allow iPod audio to continue playing in the background, you'll also want to check kAudioSessionProperty_OverrideCategoryMixWithOthers.
As of iOS 6, there is an alternative method that's more compact than Ramin's answer.
#import <AVFoundation/AVFoundation.h>
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback
error:nil];
To allow background audio from other apps to continue playing, add the AVAudioSessionCategoryOptionMixWithOthers option:
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback
withOptions:AVAudioSessionCategoryOptionMixWithOthers
error:nil];
There's further details in Apple's AVAudioSession Class Reference.
It must be, there's been a couple games I've had which (even when its in mute mode) will play sound. Unfortunately this was discovered whilst attempting to play games covertly in class.
As to how to actually do it, I really don't have any idea.