检测音乐是否正在播放,然后关闭或打开开关

时间:2013-05-22 00:13:34

标签: ios objective-c xcode4 uiswitch audio-player

我正在创建这个应用程序,如果有背景音乐播放,但我想要它,以便用户可以使用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];

}

}

1 个答案:

答案 0 :(得分:0)

yourViewController.h中的

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

- (void)registerForBackgroundNotifications;
yourViewController.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