2015-11-23 8 views
7

Ich spiele Video von der URL mit AVPlayer. AVPlayerItemDidPlayToEndTimeNotification wird nicht ausgelöst. Ich habe die Haltepunkte überprüft. Im Folgenden mein Code-Schnipsel ist: -AVPlayerItemDidPlayToEndTimeNotification nicht ausgelöst

@IBAction func playButtonClicked(sender: UIButton) { 
    let url:NSURL = NSURL(string: self.currentSelectedContent.link)! 
    moviePlayer = AVPlayer(URL: url) 
    playerViewController = AVPlayerViewController() 
    playerViewController.player = moviePlayer 

    self.presentViewController(playerViewController, animated: true) { 
     NSNotificationCenter.defaultCenter().addObserver(self, selector: "moviePlayBackFinished", name: AVPlayerItemDidPlayToEndTimeNotification, object: self.moviePlayer) 
     self.playerViewController.player?.play() 
    } 

} 

func moviePlayBackFinished() { 
    self.playerViewController.dismissViewControllerAnimated(true, completion: nil) 
} 

Antwort

5

Eigentum actionAtItemEnd von AVPlayer ist KVO-konform:

moviePlayer.addObserver(self, forKeyPath: "actionAtItemEnd", options: [], context: nil) 


override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) { 
    if keyPath == "actionAtItemEnd"{ 
     // 
     print("FINISH") 
    } 
} 

https://developer.apple.com/documentation/avfoundation/avplayer/1387376-actionatitemend

https://developer.apple.com/documentation/avfoundation/avplayeractionatitemend

14

die Dokumentation nach, muss das beobachtete Objekt sein die AVPlayerItem Instanz, nicht dieselbst. Versuchen Sie, self.moviePlayer zu self.moviePlayer.currentItem zu ändern.

+0

Dank dieses für mich gearbeitet – smoothumut

+0

Endlich eine richtige Antwort, diese zu finden war hart! Vielen Dank! – JOM

3

Dies funktioniert für mich:

NotificationCenter.default.addObserver(self, 
selector:#selector(NativeVideoPlayer.didEndPlayback), 
name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object:nil)