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."
Related
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.
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
);
I'm using Matt Gallagher's awesome AudioStreamer example to play an audio file via AudioSession. All works, except that on my iPhone 4 I get audio playback out of my bottom speaker, and not the ear speaker. I inserted the following code to no avail.
UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_None;
AudioSessionSetProperty (
kAudioSessionProperty_OverrideAudioRoute,
sizeof (audioRouteOverride),
&audioRouteOverride
);
http://developer.apple.com/library/ios/#documentation/AudioToolbox/Reference/AudioSessionServicesReference/Reference/reference.html#//apple_ref/c/econst/kAudioSessionProperty_AudioRoute
for kAudioSessionProperty_OverrideAudioRoute:
"This property can be used only with the kAudioSessionCategory_PlayAndRecord (or the equivalent AVAudioSessionCategoryRecord) category."
If you have it se to any other Audio Session category it will not work
What do you think is the best (i.e. most memory efficient) way of playing a few 30 second audio files (m4v) from the bundle within an app to the external speaker? The audio files have to play automatically as soon a specific view appears.
There is no mixing, etc. going on. However I'm listening to the microphone at the same time via AVAudioRecorder to measure a few noise levels/peak sounds etc. and react to them.
Thanks!
m4v are video files. Did you mean m4p?
Assuming you did, I'd say start with AVAudioPlayer
Since you're using input at the same time as playback, you'll want to set your Audio Session's category appropriately:
[AVAudioSession sharedInstance].category = AVAudioSessionCategoryPlayAndRecord;
// note that on the iPhone that category will send audio output to the
// phone earpiece rather than the speaker, so you have to redirect the output
// like so:
UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty (
kAudioSessionProperty_OverrideAudioRoute,
sizeof (audioRouteOverride),
&audioRouteOverride
);
In my app i record and play audio at the same time. The app is almost finished. But there is one thing, that annoying me. When audio session is set to PlayAndRecord, sounds become quiet in comparison with the same sounds with the SoloAmbient category. Is there any way to make sound louder using PlayAndRecord?
when you use the session for play and record, the playback comes out of the speaker used for the phone, otherwise it comes out the speaker located at the bottom of the phone. this is to prevent feedback. you can override this like so (but watch out for feedback, not an issue if you aren't doing both at once)
//when the category is play and record the playback comes out of the speaker used for phone conversation to avoid feedback
//change this to the normal or default speaker
UInt32 doChangeDefaultRoute = 1;
AudioSessionSetProperty (kAudioSessionProperty_OverrideCategoryDefaultToSpeaker, sizeof (doChangeDefaultRoute), &doChangeDefaultRoute);
this code works on 3.1.2, earlier sdk's you have to do differently.
UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
status = AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute, sizeof (audioRouteOverride), &audioRouteOverride);
you have to be careful with this method, it will override even if you have headphones plugged in, you have to monitor interruptions and change the routes accordingly. much better now using 3.1.2
Ask the user to plug in headphones?
The headphone + mic combination doesn't suffer from this problem.
I don't know if it's a bug, a consequence of the audio hardware,
or if the quiet playback is just an intentional and hamfisted
way of getting cleaner recordings.
UPDATE
I found out that setting the PlayAndRecord session changes your audio route to the receiver.
Apparently the use case is for telephony applications where the user holds the device up to his ear.
If that doesn't violate the Principle of Least Surprise, I don't know what does.