tags:

views:

150

answers:

1

I would like to have a setting to disable all sounds coming from my program. I know I can set a global ivar and write if statements for each sound but I was hoping there was a something I could set in the appdelegate to disable all program sounds.

I have played a couple of games that allow you to turn game sounds off.

I am using AVAudioPlayer for longer clips and basic Audio Service System Sounds for short clips.

A: 

What do you use to play sounds? I think you could disable sounds by deactivating the audio session – see AudioSessionSetActive.


Update: Yes, you’re right. I’ve just tried deactivating the audio session and the sounds seemed to go on. Nevermind. You could use the boolean flag approach, and there is no need to have a condition around every sound. The way I do SFX in my game is through a separate class, a kind of ‘view’ that observes the model and creates sounds for various game events. This way you keep a clean separation of concerns in the design and when you want to switch the sounds off, you simply disconnect the sound class from the model. The code looks a bit like this:

@implementation Model

- (void) stepBy: (double) seconds
{
     [player doSomething];
     if (player.isDead)
        [self sendNotification:@selector(playerHasDied:) withObject:player];
}

@end

And the sound view:

@implementation SFX

- (void) playerHasDied: (id) player
{
    [aarghSound play];
}

@end

Of course you have to implement the actual observing part. You could use NSNotificationCenter or write your own dispatching code using an array of observers:

@implementation Model

- (void) addObserver: (id) object
{
    [observers addObject:object];
}

- (void) sendNotification: (SEL) message
{
    for (id observer in observers)
        if ([observer respondsToSelector:message])
            [observer performSelector:message];
}

@end

The SFX view is connected to the model:

Model *model = [[Model alloc] init];
SFX *sounds = [[SFX alloc] init];
[model addObserver:sounds];

When you want to disable all sounds, you just disconnect the SFX from the model; stop observing. If the sounds are disabled before the game starts, you even do not have to allocate the SFX class – this saves time, performance and memory.

zoul
Read the documentation on this and it seam more relevant to setting audio priority. I cannot figure out how I would use it in my application.
Aaron