Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PlaySystemSound with mute switch on

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);
like image 441
samvermette Avatar asked Jun 17 '10 02:06

samvermette


1 Answers

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.

like image 89
samvermette Avatar answered Sep 27 '22 16:09

samvermette