Detect when music player is being paused

荒凉一梦 提交于 2020-01-06 13:53:25

问题


I wonder if it's possible to detect when the the music in my "iPod" on the phone is being paused, not if it's paused or currently playing, just the moment when the music is being paused. I guess i should be using AVAudio?

Thanks!


回答1:


MPMusicPlayerController has a method:

+(MPMusicPlayerController *)iPodMusicPlayer;

The iPod music player employs the iPod app on your behalf. On instantiation, it takes on the current iPod app state and controls that state as your app runs. Specifically, the shared state includes the following:

Repeat mode (see “Repeat Modes”) Shuffle mode (see “Shuffle Modes” Now-playing item (see nowPlayingItem) Playback state (see playbackState) Other aspects of iPod state, such as the on-the-go playlist, are not shared. Music that is playing continues to play when your app moves to the background.

You can check its playbackState which may be:

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

You can also get notified if its playbackState change with the 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;
    }
}


来源:https://stackoverflow.com/questions/25788671/detect-when-music-player-is-being-paused

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!