when the music application starts playing:
-(void)playBgMusic { NSString *path = [[NSBundle mainBundle] pathForResource:@"bgmusic" ofType:@"aif"]; theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; theAudio.delegate = self; [theAudio play]; }
but he should be able to turn off the music by pressing the button, if he presses the button again, the music will turn on again. I have:
-(IBAction)check { if (isquiet == NO) { [theAudio stop]; isquiet = YES; defaults = [NSUserDefaults standardUserDefaults]; [defaults setBool:YES forKey:@"stringKey"]; } else { [self playBgMusic]; isquiet = NO; defaults = [NSUserDefaults standardUserDefaults]; [defaults setBool:NO forKey:@"stringKey"]; } }
I think I did not understand this. Now it works in my first ViewController that I can turn music on and off, but when I switch to another viewController while playing music, then press the button again and again, the music does not stop and when I click it many times the music is played a second time and overlaps.
What else is wrong?
source share