如何检测AVPlayerItem何时完成播放?

我一直在查看AVPlayerItemAVPlayer文档,并且似乎没有任何callback,当项目完成播放。 我希望能有一些委托callback,我们可以利用或AVPlayerActionAtItemEnd将提供一个自定义的行动,我们写。

我怎样才能找出一种方法来检测什么时候AVPlayer已经完成播放项目?

当播放完成时,它使用NSNotification来提醒。

注册通知:

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(itemDidFinishPlaying:) name:AVPlayerItemDidPlayToEndTimeNotification object:playerItem]; 

完成时要调用的方法:

 -(void)itemDidFinishPlaying:(NSNotification *) notification { // Will be called when AVPlayer finishes playing playerItem } 

Swift-i-fied(版本3)

 class MyVideoPlayingViewController: AVPlayerViewController { override func viewDidLoad() { // Do any additional setup after loading the view. super.viewDidLoad() let videoURL = URL(fileURLWithPath: Bundle.main.path(forResource: "MyVideo", ofType: "mp4")!) player = AVPlayer(url: videoURL) NotificationCenter.default.addObserver(self, selector: #selector(MyVideoPlayingViewController.animationDidFinish(_:)), name: .AVPlayerItemDidPlayToEndTime, object: player?.currentItem) } override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) player?.play() } func animationDidFinish(_ notification: NSNotification) { print("Animation did finish") } deinit { NotificationCenter.default.removeObserver(self) } } 

这就是我做的。

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(movieFinishedCallback:) name:AVPlayerItemDidPlayToEndTimeNotification object:player.currentItem]; - (void)movieFinishedCallback:(NSNotification*)aNotification { // [self dismissViewControllerAnimated:YES completion:Nil]; }