检测音乐播放器何时暂停

时间:2014-09-11 13:24:40

标签: ios avaudioplayer

我想知道是否有可能检测到我的" iPod"电话暂停,不是暂停或正在播放,只是暂停音乐的那一刻。我想我应该使用AVAudio

谢谢!

1 个答案:

答案 0 :(得分:0)

MPMusicPlayerController有一个方法:

+(MPMusicPlayerController *)iPodMusicPlayer;
  

iPod音乐播放器代表您使用iPod应用程序。上   实例化,它采用当前的iPod应用程序状态和控件   当您的应用运行时,该状态。具体而言,共享状态包括   以下内容:

     

重复模式(参见“重复模式”)随机播放模式(参见“随机播放模式”   正在播放的项目(参见nowPlayingItem)播放状态(参见   playbackState)iPod状态的其他方面,例如移动中   播放列表,不共享。播放的音乐在播放时继续播放   你的应用程序移到后台。

您可以查看playbackState可能是:

enum {
   MPMusicPlaybackStateStopped,
   MPMusicPlaybackStatePlaying,
   MPMusicPlaybackStatePaused,
   MPMusicPlaybackStateInterrupted,
   MPMusicPlaybackStateSeekingForward,
   MPMusicPlaybackStateSeekingBackward
};
typedef NSInteger MPMusicPlaybackState;

如果playbackState更改为MPMusicPlayerControllerPlaybackStateDidChangeNotification,您也会收到通知。

@property (nonatomic, strong) MPMusicPlayerController *musicPlayer;
-(void)iPodMusicPlayer
{
    musicPlayer = [MPMusicPlayerController iPodMusicPlayer]; 
    switch ([musicPlayer playbackState]) 
    {
        case: MPMusicPlaybackStateStopped:
            NSLog(@"iPod player is stopped)";
            //Do something
            break;
        case: MPMusicPlaybackStatePaused:
            NSLog(@"iPod player is paused");
            //Do something
             break;
        case: MPMusicPlaybackStatePlaying:
            NSLog(@"iPod player is playing");
            //Do something
             break;
        //Etc.
        default:
           break;
    }

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(musicPlayerPlayBackStatusChanged:) 
                                                 name:MPMusicPlayerControllerPlaybackStateDidChangeNotification
                                                object:nil];

    [musicPlayer beginGeneratingPlaybackNotifications];
}

-(void)musicPlayerPlayBackStatusChanged:(NSNotification *)notification
{
    switch ([musicPlayer playbackState]) 
    {
        case: MPMusicPlaybackStateStopped:
            NSLog(@"iPod player is stopped)";
            //Do something
            break;
        case: MPMusicPlaybackStatePaused:
            NSLog(@"iPod player is paused");
            //Do something
             break;
        case: MPMusicPlaybackStatePlaying:
            NSLog(@"iPod player is playing");
            //Do something
             break;
        //Etc.
        default:
           break;
    }
}