AudioSessionProperty on iPhone - iphone

In my iPhone application I need to enable loud speaker and enable bluetooth.
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayAndRecord error: nil];
UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,
sizeof (audioRouteOverride), &audioRouteOverride);
UInt32 allowBluetoothInput = 1;
AudioSessionSetProperty (
kAudioSessionProperty_OverrideCategoryEnableBluetoothInput,
sizeof (allowBluetoothInput),
&allowBluetoothInput
);
But it is not working together. I mean that if bluetooth is on I hear voice from BT headset, if it is off, I hear voice not from loud speaker. How can I resolve this issue?

Have you tried changeDefaultRoute instead of audioRouteOverride for the speaker?
UInt32 doChangeDefaultRoute = 1;
AudioSessionSetProperty(
kAudioSessionProperty_OverrideCategoryDefaultToSpeaker
, sizeof(doChangeDefaultRoute)
, &doChangeDefaultRoute);
This fixes the output going to speaker instead of receiver issue, but I cannot test the bluetooth input side (no bluetooth device!). It may not as from what I can gather OverrideCategoryEnableBluetoothInput actually controls input and output.
Apple says this:
This property affects the kAudioSessionCategory_PlayAndRecord category as follows: If the audio input to the device is coming from a Bluetooth headset, setting this property to TRUE results in audio output also going to the Bluetooth headset.
(Audio Session Services Reference)
In general, Apple do not want apps to manipulate routing in ways that may be counter-intuitive to the user. The idea is that routing should be something that the user feels they have control over.

Related

AudioServicesPlaySystemSound not working in iphone while card-reader is plugged in

I am using a card reader for iPhone and I need to play a sound while the reader is plugged in. Due to some other issues I used the AudioServicesPlaySystemSound. But the sound is not playing through speaker when the reader is plugged in.
Is there any methods to change the audio channel to speaker while using AudioServicesPlaySystemSound?
UInt32 doChangeDefaultRoute = 1;
AudioSessionSetProperty (kAudioSessionProperty_OverrideCategoryDefaultToSpeaker,
sizeof (doChangeDefaultRoute),
&doChangeDefaultRoute
);
I have tried the above code. It shows some strange behaviour. I observed the following cases:
case 1: Plugged in the reader and tried to play the sound. Failed to play the sound.
case 2: Unplugged the reader and played the sound through speaker once. Then plug in the reader and tried to play the same sound. It works!
But I need to play the sound in both cases.
Note: Instead of card reader you can use any device with an audio jack. Even a head set can be used.
You can try to override sound output to speaker:
UInt32 doChangeDefaultRoute = 1;
AudioSessionSetProperty (kAudioSessionProperty_OverrideCategoryDefaultToSpeaker,
sizeof (doChangeDefaultRoute),
&doChangeDefaultRoute
);

Recording and playing audio with iPod

I'm trying to record and play audio in an universal app. The AVAudioSession configuration is:
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error: nil];
UInt32 category = kAudioSessionCategory_PlayAndRecord;
AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(category), &category);
This is working fine for devices with built-in microphone. But in a third generation iPod I am experiencing random undesired behaviours. Sometimes all works fine, and sometimes it happens only when the earphones with the integrated mic are plugged-in. In this case, is not possible to hear any sound in the app without the earphones.
The 3rd generation of iPod Touch doesn't have a built-in microphone. The only way to have one is to plug your headphones with a mic.
The only generation of iPod Touch with a built-in microphone is the 4th.
You can check if inputRecording is available :
UInt32 propertySize, micConnected;
AudioSessionGetProperty(kAudioSessionProperty_AudioInputAvailable, &propertySize, &micConnected);

iPhone AudioSession properties

I'm developing an app that should have the following properties regarding the audio:
can record and play sound at the same time
can mix the audio output with other app, e.g. iPod
audio output at speaker when earphone is not plugged in
audio output at earphone when it is plugged in
I used the following code.
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
NSError *audioSessionError;
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:&audioSessionError];
UInt32 mix = 1;
AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof(mix), &mix);
UInt32 route = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty(kAudioSessionProperty_OverrideAudioRoute, sizeof(route), &route);
[audioSession setActive:YES error:&audioSessionError];
However, I can achieve 1-3 but failed at 4. When earphone is plugged in, the audio still comes through the speaker. Then I tried setting kAudioSessionProperty_OverrideCategoryDefaultToSpeaker instead of kAudioSessionProperty_OverrideAudioRoute, but this resulted pausing the iPod instead of mixing both audio. Could anyone please help pointing out what's wrong with the above code?
Thanks for any help.
I think this:
UInt32 route = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty(kAudioSessionProperty_OverrideAudioRoute, sizeof(route), &route);
Specifically means use the speaker. The default action (to use the headphones when plugged in) should be:
UInt32 route = kAudioSessionOverrideAudioRoute_None;

iPhone audio playback: force through internal speaker?

Does anyone know if it is possible to implement playback of an audio file through the internal speaker even if the headphones are plugged in?
I'm not sure how you are doing your audio playback, but try having a look at the "Redirecting Output Audio" section of the Audio Session Programming Guide
It looks something like this:
UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker; // 1
AudioSessionSetProperty (
kAudioSessionProperty_OverrideAudioRoute, // 2
sizeof (audioRouteOverride), // 3
&audioRouteOverride // 4
);
Actually i think this is not possible, as there seems to be some kind of mechanical switch, which indicates a plugged in headset thus preventing speaker output when this is the case.
(read here)
Some other hints can be found in the description of kAudioSessionProperty_OverrideCategoryDefaultToSpeaker:
"Specifies whether or not to route audio to the speaker (instead of to the receiver) when no other audio route, such as a headset, is connected."

iPhone - AVAudioPlayer, kAudioSessionCategory_AmbientSound and iPod music

I'm using the following Audio Session in my app delegate:
AudioSessionInitialize(NULL, NULL, NULL, self);
UInt32 sessionCategory = kAudioSessionCategory_AmbientSound;
AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(sessionCategory), &sessionCategory);
AudioSessionSetActive(true);
I want the user to be able to play iPod music and use my app at the same time, which is great and works fine... my app bows out and allows the iPod music to play.
The issue I'm having is... after the user exits my app, goes into the iPod app and pauses, when they come back into my app, none of my sounds work. It's like it still thinks the iPod session is active, even though it isn't playing any music!
I basically just want to re-activate my audio session after iPod music has been paused. As it stands, after I've ever played music via the iPod app, I am totally unable to get my app's sounds back unless I recompile. :( Anyone have any ideas?
Edit: I forgot to mention I'm using a basic implementation of the AVAudioPlayer class to play my app's audio.
Thanks!
Okay just thought I'd keep everyone posted in case it helps someone else... what I did was probably kind of hackish, but seems to do the trick!
UInt32 isPlaying;
UInt32 propertySize = sizeof(isPlaying);
OSStatus status;
// check to see if their iPod music is playing
status = AudioSessionGetProperty(kAudioSessionProperty_OtherAudioIsPlaying, &propertySize, &isPlaying);
// set the session category accordingly
if(!isPlaying) {
NSLog(#"...SoloAmbientSound");
UInt32 sessionCategory = kAudioSessionCategory_SoloAmbientSound;
AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(sessionCategory), &sessionCategory);
}else{
NSLog(#"...AmbientSound");
UInt32 sessionCategory = kAudioSessionCategory_AmbientSound;
AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(sessionCategory), &sessionCategory);
}