简体   繁体   English

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

[英]Detect if music is playing or not and turn switch in off or on

I am creating this app were there is background music playing, but I want it so the user can stop the music with a UISwitch if they dont want background music. 我正在创建此应用,当时正在播放背景音乐,但是我想要它,以便用户如果不想背景音乐,可以使用UISwitch停止音乐。 I already have the code working for the music to play and stop (Code below) with the switch bu my question is this. 我已经有了用开关播放和停止音乐的代码(下面的代码),我的问题是这个。 When i switch to a different view (one that the switch isnt on) and the music is playing, then go back to the view. 当我切换到另一视图(未打开该开关)并且正在播放音乐时,请返回该视图。 The switch is off, when i turn it back on (even thought the music is already playing), it will play it again and they will overlap each other (same music file). 开关关闭,当我重新打开它(甚至认为音乐已经在播放)时,它将再次播放并且它们会彼此重叠(相同的音乐文件)。

Code for the switch and music player... 开关和音乐播放器的代码...

-(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];

}

}

in yourViewController.h 在yourViewController.h中

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

- (void)registerForBackgroundNotifications;

in yourViewController.m 在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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM