iPhone - AVAudioPlayer, kAudioSessionCategory_AmbientSound and iPod music - iphone

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

Related

iPhone:recording not hear when iphone is on silent mode

I want to hear a recorded file when my iphone is on silent mode. For that I have implemented the following code.
-(void)viewWillAppear:(BOOL)animated{
AudioSessionInitialize (NULL, NULL, NULL, NULL);
AudioSessionSetActive(true);
// Allow playback even if Ring/Silent switch is on mute
UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
AudioSessionSetProperty (kAudioSessionProperty_AudioCategory,
sizeof(sessionCategory),&sessionCategory);
}
I am unable to hear high pitched sound in iphone and in my ipad the sound effect button is enabled even when I am not able to hear any sound.
Before implementing above code I was able to hear recorded file.
If you have any idea or code then share it
thanx in advance...

iOS: Unable to play my sounds when silent mode on

So I've already gone through most of the relevant question on SO that I could find, yet I still can't get my audio playing while silent. I believe that it's probably a small error I'm making.
I'm trying to play a small alert sound. This is the code in my viewDidLoad method :
AudioSessionInitialize (NULL, NULL, NULL, NULL);
AudioSessionSetActive(true);
And here is the method I call when I need to play an audio clip
-(void)playAudioClip:(CFStringRef)clip{
CFBundleRef mainBundle = CFBundleGetMainBundle();
CFURLRef soundFile = CFBundleCopyResourceURL(mainBundle, clip, CFSTR("wav"), NULL);
//Supposed to make sound playable even when silent right?
UInt32 soundType = kAudioSessionCategory_MediaPlayback;
UInt32 soundID;
AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(soundType), &soundType);
AudioServicesCreateSystemSoundID(soundFile, &soundID);
AudioServicesPlaySystmSound(soundID);
}
I got most of this off SO and confirmed it in the Apple programming guides. I have already tried combining the Uint32 for both uses but this didn't do anything either, still no play in locked/silent mode. Any ideas?
System sounds do not play while in silent mode. (As the name would suggest) If you need to play audio while silent you need to use a different method like a media player or other audio method. Look at Apple's examples for playing music.

iOS : Music goes off while playing a sound effect

I am trying to play a short music but when iPod is playing and lunch the application music goes off ! how can I prevent this situation ? I am using AVAudioPlayer to play sound .
thanks
For AudioToolBox and and short effect sounds you can use kAudioSessionCategory
AudioSessionInitialize(NULL, NULL, NULL, self);
UInt32 category = kAudioSessionCategory_AmbientSound;
AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(category), &category);
AudioSessionSetActive(YES);
hopes can help ;)

Using AVAudioRecorder and AVAudioPlayer at the same time (or alternative ways of listening for sound and playing sound)

Is there any reason why this wouldn't work?
I'm testing for sound in the microphone and playing a looping sound effect as I'm registering sound.
On the iPod touch this seems to work fine - on the iPhone though the sound effect either doesn't play at all, or is very quiet.
Any ideas? Or any alternative?
Thanks
for some reason had to put this code before I played the sound:
UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(sessionCategory), &sessionCategory);
UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride);
to stop it playing through the earpiece.

Keeping the volume controls on even with vibrate switch on using MPMoviePlayerViewController like YouTube app does

We have an application that uses the MPMoviePlayerViewController and our customer noticed that when the vibrate switch is on, the sound is off and the volume controls are taken away. That seems to be standard behavior.
Now I was expecting the YouTube app to have the same behavior but they don't. When you turn the switch on, you can play YouTube clips and the sound is on and the volume controls are available.
Any idea on how they achieved that?
Set the audio session category for your app for media playback:
AudioSessionInitialize(NULL, NULL, NULL, NULL);
UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof (sessionCategory), &sessionCategory);
You can see the various audio categories that are available to your app here: Audio Session Categories
By having access to undocumented APIs. The Youtube app on iPhones is an Apple app, meaning they can simply do stuff that you can't.