views:

123

answers:

4

I know, I have to set the AudioSession to the 'playback' category, which allows audio even when the mute switch is on. This is what I do, but sound still gets muted when switch is on.

 UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
 AudioSessionSetProperty(kAudioSessionProperty_AudioCategory,sizeof(sessionCategory), &sessionCategory);

 SystemSoundID soundID;
 NSString *path = [[NSBundle mainBundle] pathForResource:soundString ofType:@"wav"];    

 AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:path],&soundID);
 AudioServicesPlaySystemSound (soundID);


EDIT: by the way, the app is a soundpad. Playing sound is the sole purpose of the app. Here's what Apple Doc says about this:

Use this category for an application whose audio playback is of primary importance. Your audio plays even with the screen locked and with the Ring/Silent switch set to silent.


EDIT 2: with the mute switch on, sound wont even play through the headphones. I know the user is king. I know the mute switch has its purpose. That is not the question. I'm trying to get an answer on the fact that setting the AudioSession category to kAudioSessionCategory_MediaPlayback doesn't have the expected result.


EDIT 3: following Jonathan Watmough's suggestion, I set the AudioServices kAudioServicesPropertyIsUISound property, but still no luck. Am I missing something?

// set the session property
UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
AudioSessionSetProperty(kAudioSessionProperty_AudioCategory,sizeof(sessionCategory), &sessionCategory);

// creates soundID
SystemSoundID soundID;
NSString *path = [[NSBundle mainBundle] pathForResource:soundString ofType:@"wav"];    
AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:path],&soundID);

// Jonathan Watmough suggestion
UInt32 flag = 0;
AudioServicesSetProperty(kAudioServicesPropertyIsUISound, sizeof(UInt32), &soundID, sizeof(UInt32), &flag);

AudioServicesPlaySystemSound (soundID);
+1  A: 
Jonathan Watmough
Thought that was it but nope :( See edit 3 in my original post
Sam V
Yea that's what I was thinking. Please emphasize the answer on the fact that it can't be done with SystemSound (suggesting OpenAL) so I can accept your answer. Thanks a lot for helping out!
Sam V
A: 

You are calling setActive:error: for the AVAudioSession somewhere right? Here is the code that I use to initialize. But I'm not using system sounds, I'm using AVAudioPlayer to play sounds.

avSession = [AVAudioSession sharedInstance];    // init session, important

// allow other audio to mix, such as iPod
const UInt32 categoryProp = kAudioSessionCategory_MediaPlayback;
AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(categoryProp), &categoryProp);

UInt32 category = kAudioSessionCategory_MediaPlayback;
AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(category), &category);

// Allow our audio to mix with any iPod audio that is playing
AudioSessionSetProperty (kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof(setProperty_YES), &setProperty_YES);

[avSession setActive:YES error:nil];
progrmr
A: 

I cannot find it explicitly mentioned in the docs, but I would expect the AudioServicesPlaySystemSound() ignores your audio session configuration.

If you want to control the session mixing and other properties you need to use AVAudioPlayer instead e.g.:

NSError *err;
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayback error:&err];
[session setActive:YES error:&err];

NSString *path = [[NSBundle mainBundle] pathForResource:@"Basso" ofType:@"aiff"];    
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:&err];
[player prepareToPlay];
[player play];
Crufty
I'm playing super short sounds that shouldn't be played with AVAudioPlayer. AudioServicesPlaySystemSound or OpenAL are the 2 appropriate sound engines for playing short sounds.
Sam V
Well you cannot use AudioServicesPlaySystemSound as the "system" will be controlling the session mixing options, including muting. I cannot see a way to circumvent that, although it would be pretty handy at times to be able to do so.
Crufty
A: 

Well to add on Jonathan Watmough's answer, indeed it doesn't seem possible to let AudioServicesSystemSound overwrite the mute switch. What I ended up doing is using OpenAL to play the sounds, which will play just fine following the Audio Session category you specify. Here's how I setup the Audio session:

AudioSessionInitialize(NULL, NULL, NULL, NULL);
UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
AudioSessionSetProperty(kAudioSessionProperty_AudioCategory,sizeof(sessionCategory), &sessionCategory);
AudioSessionSetActive(YES);

To play the sounds, I used Finch, a simple OpenAL-based sound effect player for iPhone.

Sam V