Playing Audio in an iPhone App - iphone

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

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. */

iphone play and record low volume problem

I'm making kind of voip app, so I have to be able to play and record sound at the same time.
But.. when I play and record at same time, iphone's volume was very low.
I used
//kAudio
UInt32 audioCategory = kAudioSessionCategory_PlayAndRecord;
AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof(audioCategory), &audioCategory);
and
I try to
UInt32 audioRoute = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty(kAudioSessionProperty_OverrideAudioRoute, sizeof(audioRoute), &audioRoute);
but.. still volume is very low..
please help..!
P.S
I'm using playing sound by audioQueue and recording sound by audioUnit.
Have you set your volume using kAudioSessionProperty_InputGainScalar property prior to recording ?
BTW, this input gain property is only available from iOS5.

iPhone SDK audioSession question

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.

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: How to detect if iTunes is playing?

I noticed that some apps programmatically mute itunes (if its running) at launching. How is this achieved? I have a game with background music and would like to either stop itunes or get at least a message that itunes is playing so that I can stop the game's background music.
thx,
marc.
You don't need to. With Audio Session you can decide how the audio should behave.
From the Audio Session Programming Guide:
With the audio session interface, you
specify aspects of your application’s
audio behavior and configure it to
live harmoniously within the iPhone
audio environment. You start by asking
yourself questions such as these:
Do you want your audio to be silenced by the Ring/Silent switch?
The answer is probably “yes” if audio
is not essential to using your
application successfully. (Users will
appreciate being able to run your game
in a meeting with no one the wiser.)
Do you want iPod audio to continue playing when your audio
starts? This could be appropriate for
a virtual piano, letting users play
along to songs in their libraries.
You’d want iPod audio to stop,
however, for a streaming radio
application.
You probably want this:
UInt32 sessionCategory = kAudioSessionCategory_SoloAmbientSound;
AudioSessionSetProperty (
kAudioSessionProperty_AudioCategory,
sizeof (sessionCategory),
&sessionCategory
);
For more behaviour types, check the Audio Session Categories, or read the entire Audio Session Programming Guide.
I had the opposite problem. My app plays a short video with no sound after launch. This caused the iTunes music playing in the background to mute.
In order to keep the music playing, I add this in the applicationDidFinishLaunching:
NSError* error;
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryAmbient error: &error];
if (error) NSLog(#"Unable to configure Audio");