-->

MPMoviePlayerController - Detect and Differ Prev/N

2019-08-13 03:57发布

问题:

I've achieved detecting the click of prev/next button by following code, but still haven't found an way to distinguish the two clicks.

in @implementation MyMovieController : MPMoviePlayerController

[[NSNotificationCenter defaultCenter]
     addObserver:self selector:@selector(movieChangeCallBack:)
     name:MPMoviePlayerPlaybackStateDidChangeNotification object:nil];

and define - (void) movieChangeCallBack:(NSNotification*) aNotification

- (void)movieChangeCallBack:(NSNotification*) aNotification {

    if (self.playbackState == MPMoviePlaybackStateStopped)
    {
        //Touched 'Previous' or 'Next' button.
    }
}

Is there a way to tell whether the 'previous' or 'next' button is clicked? Thanks :)

回答1:

Unfortunately, MPMoviePlayerController, by default, fires off MPMoviePlayerPlaybackStateDidChangeNotification when either the Prev or Next is tapped. There's no way to uniquely be notified whether each one is tapped.

The only way I found, was to create my own custom controls for backward and forward, adding a target to it to perform an action:

[prevBtn addTarget:self action:@selector(onClick:) 
forControlEvents:UIControlEventTouchUpInside];

[nextBtn addTarget:self action:@selector(onClick:) 
forControlEvents:UIControlEventTouchUpInside];

Then in your onClick method:

 (void)onClick:(UIButton*)sender
 {
     if (sender == prevBtn)
     {
        // Do whatever when prevBtn is tapped
     }
     else if (sender == nextBtn)
     {
        // Do whatever when nextBtn is tapped
     }    
 }

FYI: you must set the player's controlStyle property to MPMovieControlStyleNon to hide the default controls.



回答2:

MPMoviePlayerController/MPMoviePlayerPlaybackStateDidChangeNotification NS_DEPRECATED_IOS(3_2, 9_0) is deprecated. You should switch over to AVPlayer.