9

Używam MPMoviePlayerController, w jaki sposób mogę wykryć, kiedy film faktycznie zaczął się odtwarzać - w przeciwieństwie do sytuacji, w której użytkownik manipuluje mechanizmami wyszukiwania?Jak wykryć MPMoviePlayerController uruchamia film?

Od testów zrobiłem, zawsze dostać „zmiana stanu obciążenia” zdarzenie i (moviePlayer.loadState == MPMovieLoadStatePlayable) jest TRUE gdy zaczyna filmie i gdy użytkownik przeciągnął poszukują kontroli (nawet jeśli wlókł go od początku do połowy - niekoniecznie początek filmu). Jak odróżnić rozpoczęcie filmu od poszukiwania?

+0

Może [to] (https://developer.apple.com/library/ ios/documentation/mediaplayer/reference/MPMoviePlayerController_Class/Refer ence/Reference.html # // apple_ref/doc/c_ref/MPMoviePlaybackState) pomaga? – AlexR

+0

Sprawdź to http://stackoverflow.com/questions/5787394/find-out-if-mpmovieplayercontroller-is-currently-playing – karthika

Odpowiedz

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

Rejestracja na MPMoviePlayerPlaybackStateDidChangeNotification

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

Sprawdź w tej funkcji 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 
     } 

} 

Usuń powiadomienia o

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

Patrz: MPMoviePlaybackState

+0

to również wykrywa wznowienie ze stanu pauzy - nie tylko wtedy, gdy film faktycznie się zaczyna. –

+0

@ sagimann- zobacz mój zaktualizowany ans –

+0

Dlaczego nie jest to poprawna odpowiedź? – Pier

1

Dla szybkiej

Dodaj obserwator

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

Funkcja

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) 
    }