iOS: Unable to play my sounds when silent mode on - iphone

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.

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

AVAudioSession category not working as documentation dictates

I have an iOS app that has some audio feedback in certain places, but I want any other music the user has playing in the background to be allowed to play over this. In addition, I want the audio in my app to respect the mute switch. According to the developer documentation, this functionality should all be enabled by the AVAudioSession ambient category. This is the code I'm using:
if (!hasInitialisedAudioSession) {
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryAmbient error:NULL];
[session setActive:YES error:NULL];
hasInitialisedAudioSession = YES;
}
The code is executing just fine, and it does indeed let the app sounds play over iPod music. What it doesn't do, however, is respect the mute switch. I've tried swapping this code out for similar C audio calls (stuff like AudioSessionSetProperty) instead of the Objective-C calls, but I get the same result - the ambient session category simply doesn't want to respect the mute switch, despite what the documentation says it should be doing.
Any ideas? Thanks for the help :)
I think I managed to work it out - turns out that it has nothing to do with my app at all, but rather the iPod app. My app obeys the mute switch as it should when the iPod isn't playing, and then allows the iPod to play over it - all behaviour I wanted. However, when the iPod is playing, the app stops responding to the mute switch, so I think it's just something the iPod does to the device audio settings. I could probably work a way around it if I really wanted to spend the time on it, but as long as it obeys the mute switch when the iPod isn't playing that's good enough for me.
EDIT: to work around this, just use this function to determine whether or not the mute switch is on manually, and don't play your sounds if the result is YES. Could be a bit of a pain if you don't have a central audio manager class, though. It would be nice if Apple could publish this behaviour in their documentation.
- (BOOL)deviceIsSilenced
{
#if TARGET_IPHONE_SIMULATOR
// return NO in simulator. Code causes crashes for some reason.
return NO;
#endif
CFStringRef state;
UInt32 propertySize = sizeof(CFStringRef);
AudioSessionInitialize(NULL, NULL, NULL, NULL);
AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &propertySize, &state);
return (CFStringGetLength(state) <= 0);
}

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.

How can I replace iPod with my app icon while my app is playing audio in background?

I (and you) know how to play audio in background.
But my question is like that,
some music play apps replace iPod icon which is shown on the first background app page with their icon while they are playing audio in background.
How can I do that?
We can do it using AudioSession like the following code.
AudioSessionInitialize (
NULL, // 'NULL' to use the default (main) run loop
NULL, // 'NULL' to use the default run loop mode
NULL, // a reference to your interruption callback
NULL // data to pass to your interruption listener callback
);
UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
AudioSessionSetProperty ( kAudioSessionProperty_AudioCategory, sizeof (sessionCategory), &sessionCategory );
AudioSessionSetActive(true);
Here's the documentation:
http://developer.apple.com/library/ios/#documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/RemoteControl/RemoteControl.html
Notice however, that it'll work only when you have active audio session in your application.
I'm using it with AVAudioSession with AVAudioSessionCategoryPlayback category and AVAudioPlayer and "remote controls" work only when I have AVAudioSession active and AVAudioPlayer object created.

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