禁用所有程序声音 [英] Disable all program sounds

查看:66
本文介绍了禁用所有程序声音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想进行设置以禁用程序发出的所有声音.我知道我可以设置一个全局ivar并为每种声音编写if语句,但是我希望可以在appdelegate中设置一些功能来禁用所有程序声音.

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.

我正在将AVAudioPlayer用于较长的剪辑,并将基本的音频服务系统声音用于短剪辑.

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

推荐答案

您使用什么来播放声音?我认为您可以通过禁用音频会话来禁用声音-请参阅

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

更新:是的,您是对的.我刚刚尝试停用音频会话,但声音似乎还在继续.没关系.您可以使用布尔标志方法,并且不需要在每个声音周围都有条件.我在游戏中做SFX的方法是通过一个单独的类,即一种视图",它观察模型并为各种游戏事件创建声音.这样,您就可以将设计中的关注点清晰地分开,并且当您想要关闭声音时,只需将声音类别与模型断开即可.代码看起来像这样:

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

和声音视图:

@implementation SFX

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

@end

当然,您必须实现实际的观察部分.您可以使用NSNotificationCenter或使用观察者数组编写自己的调度代码:

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

SFX视图已连接到模型:

The SFX view is connected to the model:

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

要禁用所有声音时,只需将SFX与模型断开连接;停止观察.如果在游戏开始前禁用了声音,您甚至不必分配SFX类-这样可以节省时间,性能和内存.

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.

这篇关于禁用所有程序声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆