切换开关以关闭应用程序声音 [英] Toggle switch to turn off application sounds

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

问题描述

我的应用程序中有多个声音.我想在设置中设置一个切换开关来关闭这些声音.这是执行声音的代码.

I have multiple sounds in my application. I want to setup a toggle switch in settings to turn these sounds off. Here is the code that executes the sounds.

- (void) soundEventDidHappen:(NSString*)eventName {
//check dictionary of sounds.. if there is a corresponding sound for this event name, play it

if ([[soundIDForEventString allKeys] containsObject:eventName]) {
    AudioServicesPlaySystemSound ([[soundIDForEventString objectForKey:eventName] intValue]);
}

推荐答案

您可以像这样进行简单的切换:

You could make a simple toggle like this:

bool isToggled;

- (IBAction) toggleSound{
    if(isToggled){
        isToggled = NO; //sets isToggled to false if it's already true
    }
    else{
        isToggled = YES; //sets isToggled to true if it's already false
    }
}

然后你可以这样做:

- (void) soundEventDidHappen:(NSString*)eventName {
    //check dictionary of sounds.. if there is a corresponding sound for this event name, play     it
    if(isToggled){

        if ([[soundIDForEventString allKeys] containsObject:eventName]) {
        AudioServicesPlaySystemSound ([[soundIDForEventString objectForKey:eventName] intValue]);
        }
    }
}

如果 isToggled 为真,它将播放声音.如果您想为将来保存布尔值,则可以使用 NSUserDefaults:

Which will play the sound if isToggled is true. You could then use NSUserDefaults if you would like to save the boolean for the future:

[[NSUserDefaults standardUserDefaults] setBool:isToggled forKey:@"soundEnabled"];
[[NSUserDefaults standardUserDefaults] synchronize];

//use this to save

然后您可以使用它来获取值(很可能在您的 viewDidLoad 方法中的某处:

Then you could use this to get the value (most likely somewhere in your viewDidLoad method:

isToggled = [[NSUserDefaults standardUserDefaults] boolForKey:@"soundEnabled"];
[[NSUserDefaults standardUserDefaults] synchronize];

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

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