如何让AVPlayer的NSNotification开始播放并完成播放? swift 2.2

时间:2016-08-22 04:44:41

标签: swift avplayer nsnotifications

当我的AVPlayer开始播放和完成播放时,如何获得通知,以及我在哪里放置此代码,因为AVPlayer没有直接控制器,它在segues中运行? (我是初学者。我已经在我的ViewController中尝试了以下代码来完成游戏通知,但它没有向我的控制器打印通知。任何想法,为什么它不起作用?我&#39 ;我还不确定我可以使用什么通知功能来开始播放'?

// MARK: - Navigation function

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    if segue.identifier == "seguePlayVideo" {

        // get destination view controller
        let destVc = segue.destinationViewController as! AVPlayerViewController

        // set player
        destVc.player = AVPlayer(URL: self.videoUrl)
        destVc.player?.play()
        print("av playing")

        NSNotificationCenter.defaultCenter().addObserver(self, selector: "playerItemDidReachEnd:", name: AVPlayerItemDidPlayToEndTimeNotification,object:destVc.player)

    }

    func playerItemDidReachEnd(notification: NSNotification) {
        print("av ended")
        let p: AVPlayerItem = notification.object as! AVPlayerItem
        p.seekToTime(kCMTimeZero)

}


}

}

0 个答案:

没有答案