Search code examples
iosavaudioplayer

Detect when music player is being paused


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!


Solution

  • 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;
        }
    }