Search code examples
iosiphoneobjective-cmpmovieplayercontroller

Handling Player buttons in MPMoviePlayer


I have added this code:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadMoviePlayerStateChanged:)
                                             name:MPMoviePlayerLoadStateDidChangeNotification
                                           object:self.mp];

It fires every state change of my MPMoviePlayer, into this function:

- (void) loadMoviePlayerStateChanged:(NSNotification*)notification
{

    MPMoviePlayerController *Player = notification.object;

    MPMoviePlaybackState playbackState = Player.playbackState;
    if (playbackState == MPMoviePlaybackStateSeekingForward)
    {
        NSLog(@"Forward");
    }
    else if (playbackState == MPMoviePlaybackStateSeekingBackward)
    {
        NSLog(@"Backward");
     }
}

It enters this function...

But problem is with MPMoviePlaybackState playbackState = Player.playbackState;

playerState is 1 when i launch player, and all other changes give 0. - How can that be?

Edit:

Implementing this also each time gives nil:

NSNumber *reason = [notification.userInfo objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey];

if ([reason intValue] == MPMoviePlaybackStateSeekingForward) {

    // done button clicked!

}

Solution

  • Problem is that you are adding notification for loadstate change of movie player and trying to access its playback state if you want playback state change notification you need to add observer for playback change notification

    //for playback change

    [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(playbackChanged)
                                                         name:MPMoviePlayerPlaybackStateDidChangeNotification object:self.mp];
    
    
    
    
    - (void)playbackChanged {
    
    switch ([self.mp playbackState]) {
        case MPMoviePlaybackStateStopped:
            NSLog(@"Stopped")
            break;
        case MPMoviePlaybackStatePlaying:
            NSLog(@"Playing");
            break;
        case MPMoviePlaybackStatePaused:
            NSLog(@"Paused");
            break;
        case MPMoviePlaybackStateInterrupted:
            NSLog(@"Interrupted");
            break;
        case MPMoviePlaybackStateSeekingForward:
           NSLog(@"Seeking Forward");
            break;
        case MPMoviePlaybackStateSeekingBackward:
           NSLog(@"Seeking Backward");
            break;
        default:
            break;
    }
    }