AudioStreamer playback from ear-speaker instead of bottom speaker on iPhone - iphone

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

Related

Record and play louder volume avaudio

I used the tutorial found here to record and playback a sound. The recording and playback work fine, but I can barely here the sound when I play it back. I've tried the following code:
UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,
sizeof (audioRouteOverride),&audioRouteOverride);
I add it to my "play" method, right before I play the sound. I've also tried to add it to my viewDidLoad. Both of them make it so I can't here the playback at all. It seems to be working for others, but where should I add it?
I checked out SpeakHere, Apples example AVAudio app. The volume is very quiet. When I use the Voice Memos app, the recording is played back very very loud. Has anyone been able to play a recorded message and play it back that loud?
Try setting this property of AVAudioPlayer ...
#property float volume; /* The volume for the sound. The nominal range is from 0.0 to 1.0. */

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
);

Can I Play an audio while recording a video on iPhone 4?

I'm trying to record a video while the iPhone playback an audio track.
as far as i tried, whenever I simulataniously record and play - the camera get stuck.
I went over the AVFoundation PG But I could not find a specific answer.
Is it a matter of digging into this framework?
Is it a matter of threading and multitasking?
thanks
I record both audio and video while playing audio. However, in my setup I am using CoreAudio for audio and AVFoundation for video. You may want to setup the audio session to handle recording and playback. e.g.
AudioSessionInitialize(NULL, NULL, NULL, self);
UInt32 audioCategory = kAudioSessionCategory_PlayAndRecord;
AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(audioCategory), &audioCategory);
AudioSessionSetActive(YES);

Playing Audio in an iPhone App

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
);

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."