9

J'utilise MPMoviePlayerController, comment puis-je détecter quand le film a réellement commencé à jouer - par opposition à quand l'utilisateur tripote les commandes de recherche? A partir des tests que j'ai faits, j'ai toujours un événement "load state change" et (moviePlayer.loadState == MPMovieLoadStatePlayable) est TRUE à chaque fois que le film démarre ET après que l'utilisateur a fait glisser le contrôle de recherche (même s'il l'a fait glisser de la fin au milieu - pas nécessairement le début du film). Comment faire la distinction entre film-start et seek?Comment détecter MPMoviePlayerController démarre un film?

+0

Peut-être que [ce] (https://developer.apple.com/library/ ios/documentation/mediaplayer/reference/MPMoviePlayerController_Class/Référer/Reference.html #// apple_ref/doc/c_ref/MPMoviePlaybackState) aide? – AlexR

+0

vérifier cela http://stackoverflow.com/questions/5787394/find-out-if-mpmovieplayercontroller-is-currently-playing – karthika

Répondre

28
MPMoviePlaybackState 
    Constants describing the current playback state of the movie player. 
    enum { 
     MPMoviePlaybackStateStopped, 
     MPMoviePlaybackStatePlaying, 
     MPMoviePlaybackStatePaused, 
     MPMoviePlaybackStateInterrupted, 
     MPMoviePlaybackStateSeekingForward, 
     MPMoviePlaybackStateSeekingBackward 
    }; 
    typedef NSInteger MPMoviePlaybackState; 

Inscrivez-vous au MPMoviePlayerPlaybackStateDidChangeNotification

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

Vérifiez dans cette fonction MPMoviePlaybackState

- (void)MPMoviePlayerPlaybackStateDidChange:(NSNotification *)notification 
    { 
     if (player.playbackState == MPMoviePlaybackStatePlaying) 
     { //playing 
     } 
     if (player.playbackState == MPMoviePlaybackStateStopped) 
     { //stopped 
     }if (player.playbackState == MPMoviePlaybackStatePaused) 
     { //paused 
     }if (player.playbackState == MPMoviePlaybackStateInterrupted) 
     { //interrupted 
     }if (player.playbackState == MPMoviePlaybackStateSeekingForward) 
     { //seeking forward 
     }if (player.playbackState == MPMoviePlaybackStateSeekingBackward) 
     { //seeking backward 
     } 

} 

notification Supprimer par

[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:nil]; 

Se référer: MPMoviePlaybackState

+0

cela détecte également la reprise d'un état de pause - pas seulement lorsque le film commence réellement. –

+0

@ sagimann- voir mes mises à jour ans –

+0

Pourquoi est-ce pas la bonne réponse? – Pier

1

Pour rapide

Ajouter observateur

let defaultCenter: NSNotificationCenter = NSNotificationCenter.defaultCenter() 
defaultCenter.addObserver(self, selector: "moviePlayerPlaybackStateDidChange:", name: MPMoviePlayerPlaybackStateDidChangeNotification, object: nil) 

Fonction

func moviePlayerPlaybackStateDidChange(notification: NSNotification) { 
     let moviePlayerController = notification.object as! MPMoviePlayerController 

     var playbackState: String = "Unknown" 
     switch moviePlayerController.playbackState { 
     case .Stopped: 
      playbackState = "Stopped" 
     case .Playing: 
      playbackState = "Playing" 
     case .Paused: 
      playbackState = "Paused" 
     case .Interrupted: 
      playbackState = "Interrupted" 
     case .SeekingForward: 
      playbackState = "Seeking Forward" 
     case .SeekingBackward: 
      playbackState = "Seeking Backward" 
     } 

     print("Playback State: %@", playbackState) 
    } 
Questions connexes