0

如果有背景音乐播放,我正在创建这个应用程序,但我想要它,以便用户可以在不想要背景音乐的情况下使用 UISwitch 停止音乐。我已经有了使用开关播放和停止音乐的代码(下面的代码),但我的问题是这个。当我切换到不同的视图(开关未打开的视图)并且正在播放音乐时,然后返回视图。开关关闭,当我重新打开它时(即使音乐已经在播放),它会再次播放并且它们会相互重叠(相同的音乐文件)。

开关和音乐播放器的代码...

-(IBAction)play:(id)sender {
if (audioControlSwitch.on) {

[sound setTextColor:[UIColor blueColor]];
[sound setText:@"Sound On"];

NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/Tone 2.m4a", [[NSBundle mainBundle] resourcePath]]];

NSError *error;
audioPlayer1 = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer1.numberOfLoops = 100000000000000000;

[audioPlayer1 play];
} else {

[sound setTextColor:[UIColor darkGrayColor]];
[sound setText:@"Sound Off"];

[audioPlayer1 stop];

}

}
4

1 回答 1

0

在你的ViewController.h

@interface yourViewController : NSObject <AVAudioPlayerDelegate> { 
    BOOL    inBackground;
}

- (void)registerForBackgroundNotifications;

在你的ViewController.m

@synthesize inBackground;

#pragma mark background notifications
- (void)registerForBackgroundNotifications
{
    [[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(setInBackgroundFlag)
                                             name:UIApplicationWillResignActiveNotification
                                           object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(clearInBackgroundFlag)
                                             name:UIApplicationWillEnterForegroundNotification
                                           object:nil];
}

- (void)setInBackgroundFlag
{
    inBackground = true;
}

- (void)clearInBackgroundFlag
{
    inBackground = false;
}

- (void)updateViewForPlayerStateInBackground:(AVAudioPlayer *)p
{
    if (p.playing)
    {
    // Do something
    }
    else
    {
    // Do something else
    }
}

#pragma mark AVAudioPlayer delegate methods
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)p successfully:(BOOL)flag
{
    if (flag == NO)
    NSLog(@"Playback finished unsuccessfully");
        [p setCurrentTime:0.];
if (inBackground)
{
        [self updateViewForPlayerStateInBackground:p];
}
else
{
}
}

- (void)playerDecodeErrorDidOccur:(AVAudioPlayer *)p error:(NSError *)error
{
NSLog(@"ERROR IN DECODE: %@\n", error);
}

// we will only get these notifications if playback was interrupted
- (void)audioPlayerBeginInterruption:(AVAudioPlayer *)p
{
    NSLog(@"Interruption begin. Updating UI for new state");
    // the object has already been paused,  we just need to update UI
    if (inBackground)
    {
         [self updateViewForPlayerStateInBackground:p];
    }
    else
    {
    }
}

- (void)audioPlayerEndInterruption:(AVAudioPlayer *)p
{
NSLog(@"Interruption ended. Resuming playback");
[self startPlaybackForPlayer:p];
}

-(void)startPlaybackForPlayer:(AVAudioPlayer*)p
{
if ([p play])
{

}
else
    NSLog(@"Could not play %@\n", p.url);
}

@end
于 2013-05-22T22:01:51.703 回答