AVPlayer识别播放结束

时间:2016-06-05 14:08:59

标签: swift avplayer

let url:NSURL = NSURL(string: jsonSession[indexPath.row].link)!
        let player = AVPlayer(URL: url)
        player.allowsExternalPlayback = true
        player.usesExternalPlaybackWhileExternalScreenIsActive = true
        let playerViewController = AVPlayerViewController()
        playerViewController.player = player
        self.presentViewController(playerViewController, animated: true) {
            playerViewController.player!.play()
        }

这是我的代码,我尝试推荐如果播放结束,播放下一部电影: - )

1 个答案:

答案 0 :(得分:0)

最简单的选择是使用AVQueuePlayer

let items:[AVPlyerItem] = []
for session in jsonSession {
  guard let url = NSURL(string:session.link) else { continue }
  let item = AVPlayerItem(URL:url)
  items.add(item)
}
let player = AVQueuePlayer(items: items)
//now you have the player, do what you were doing before

第二个选项是在AVPlayer上执行Key-Value Observing。它可以做类似

的事情
let url:NSURL = NSURL(string: jsonSession[indexPath.row].link)!
let player = AVPlayer(URL: url)
NSNotiicationCenter.defaultCenter().addObserver(self, selector#Selector(ViewController.itemDidFinishPlaying), name: AVPlayerItemDidPlayToEndTimeNotification, object: player.currentItem)
//I assume your ViewController class has a method called itemDidFinishPlaying

这些是粗略的代码示例,根据需要进行解释。