问题描述
编辑
我已经得到了我的应用程序一个声音,开始时,应用程序启动时播放。进一步的,我有两个方法来播放和停止的声音:
i've got a sound in my app which begins to play when the app is started. further I've got two method to play and stop the sound:
-(void)playBgMusic {
NSString *path = [[NSBundle mainBundle] pathForResource:@"bgmusic" ofType:@"aif"];
theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate = self;
[theAudio play];
[defaults setBool:NO forKey:@"isQuiet"]; }
-(void)stopMusic {
[theAudio stop];
[defaults setBool:YES forKey:@"isQuiet"]; }
现在我有不同的viewControllers在我MAINVIEW有一个按钮,停止/启动音乐(取决于wheter音乐播放或不)。
Now I've got different viewControllers and in my mainView there's a button which stops/starts the music (depends on wheter music is playing or not).
所以我有:
-(IBAction)check {
isquiet = [defaults boolForKey:@"isQuiet"];
if (isquiet == YES) {
[self playBgMusic];
// Change button to indicate music is playing
}
else {
[self stopMusic];
// Change the button to indicate music has stopped...
}
}
现在有一个问题。当应用程序启动时,声音播放,在那之后我可以preSS按钮,声音停止了,但后来我不能再次启动它。我已经把NSLogs在code,只见那布尔依然没有pressing的STOPBUTTON后。
Now there's a problem. The sound plays when the app is started, after that I can press the button and the sound is stopped but then I cant start it again. I've put NSLogs in the code and saw that the BOOL is still NO after pressing the stopButton.
哪里是我的错?
推荐答案
您在正确的轨道上,缺少的是实际保存bool值回NSUserDefaults的唯一的事情,当你开始/停止播放,所以每次你点击该按钮读它,你就会得到正确的值。
You are on the right track, the only thing missing is actually saving the bool value back to NSUserDefaults when you start/stop playing, so everytime you click the button and read it, you get the correct value.
给这个一个尝试,看看是否有帮助:
Give this a try and see if it helps:
-(IBAction)check
{
BOOL isQuiet = [userDefaults boolForKey:@"isQuiet"];
if (isQuiet)
{
[self playBgMusic];
// Change button to indicate music is playing
} else {
[self stopBgMusic];
// Change the button to indicate music has stopped...
}
}
然后在你的 playBgMusic
方法,添加以下内容:
[userDefaults setBool:NO forKey:@"isQuiet"];
而在你的 spotBgMusic
方法,添加以下内容:
[userDefaults setBool:YES forKey:@"isQuiet"];
这篇关于布尔节能问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!