ios:应用程序进入前台后,静音按钮不起作用

时间:2015-04-18 21:09:53

标签: ios xcode button foreground mute

我创建了一个按钮,可以在单击时停止并播放背景音乐。它工作得很好,但当我点击主页按钮,然后重新启动应用程序(背景 - 前景)时按钮不起作用。我点击它,它什么也没做。请帮忙!!感谢

这是代码:

- (IBAction)muteButtonPressed:(id)sender {
    UIImage *currentImage = [self.muteButton currentImage];
    UIImage *vol1 = [UIImage imageNamed:@"Volume 1"];
    UIImage *vol2 = [UIImage imageNamed:@"Volume 2"];
    if ([currentImage isEqual:vol1]) {
    [self.muteButton setImage:vol2 forState:UIControlStateNormal];
    [self.backgroundMusic stop];
    self.scene.playSounds = NO;
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setInteger:0 forKey:@"playSounds"];
    [defaults synchronize];

    }
    if ([currentImage isEqual:vol2]) {
    [self.muteButton setImage:vol1 forState:UIControlStateNormal];
        [self playBackground];
    self.scene.playSounds = YES;

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setInteger:1 forKey:@"playSounds"];
    [defaults synchronize];
    }
}

这是viewcontroller中的代码:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *playSoundsString = [defaults objectForKey:@"playSounds"];
NSInteger playSoundsInt = [playSoundsString intValue];
NSNumber *playSoundsNumber = [NSNumber numberWithInteger:playSoundsInt];
self.scene.playSounds = [playSoundsNumber boolValue];

if (self.scene.playSounds == YES) {
UIImage *vol1 = [UIImage imageNamed:@"Volume 1"];
self.scene.playSounds = YES;
[self playBackground];
[self.muteButton setImage:vol1 forState:UIControlStateNormal];
}
else if (self.scene.playSounds == NO) {
UIImage *vol2 = [UIImage imageNamed:@"Volume 2"];
self.scene.playSounds = NO;
[self.muteButton setImage:vol2 forState:UIControlStateNormal];
}

最后这是我的应用程序进入后台(在viewcontroller中):

if (self.scene.playSounds == YES) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(stopMusic:) name:@"SOUND_OFF" object:nil];
}

0 个答案:

没有答案