views:

40

answers:

2

Hi all,

I have a question regarding sounds in my app. I need the user to be able to mute all sounds coming from my game, for example if they just want to listen to the ipod while playing. There is a similar question here http://stackoverflow.com/questions/1147555/disable-all-program-sounds but there doesnt seem to be an answer. At the moment I have an AVAudioSession set to AVAudioSessionCategoryAmbient which allows the ipod to play but also will allow my app to play game sounds. Is the best way to achieve my aim to just set a boolean bhen a mute button is clicked and check this each time a sound should be played? This seems kinda awkward although it would work... any ideas please?

Many thanks

Jules

A: 

If the iPod is playing when the game begins, you can mute the sounds automatically.

e.g.,

UInt32 userPlayback;
UInt32 propertySize = sizeof(userPlayback);
AudioSessionGetProperty(
    kAudioSessionProperty_OtherAudioIsPlaying,
    &propertySize, &userPlayback);
BOOL userMusicPlaying = (userPlayback != 0);

Then if userMusicPlaying is YES, don't let the sounds play. (Consider also adding a switch that lets the sounds play even with the iPod playing.) Otherwise, I don't think there's a way to disable the sounds without you or the user disabling them.

Arseniy Banayev
A: 

Put isMuted in the user defaults

if ([[NSUserDefaults standardUserDefaults] boolForKey:@"isMuted"]) {
    playSound()....
}

to mute:

[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"isMuted"];

If you call the following it will write the settings to disk and remember what the user did last time:

[[NSUserDefaults standardUserDefaults] synchronize];
Matt Williamson
That doesn't solve Jules' problem of automatically muting all sounds coming from the game. Your answer only stores a preference, which should occur *after* the game automatically mutes all sounds, i.e., *after* his problem is solved.
Arseniy Banayev
No. The idea is to check the setting to make sure it is not muted before playing the sounds.
Matt Williamson
But Jules is asking how to automatically set that setting to NO (or whatever), not how to check the setting. He wants an automatic NO when music is playing, for example. He wants a way to set that setting.
Arseniy Banayev
Ok thanks for all your help.... just to clarify, what I was looking for is a way for the user to be able to mute in app sounds through a button click, regardless of whether or not the ipod is playing... sorry for any confusion.
Jules
Thanks for the update Jules
Matt Williamson