我一直在查看AVPlayerItem
和AVPlayer
文档,并且似乎没有任何关于项目完成播放的回调。我希望我们可以使用某种委托回调,或者AVPlayerActionAtItemEnd
会为我们提供自定义操作。
我怎样才能找到一种方法来检测AVPlayer何时播放完一个项目?
答案 0 :(得分:43)
播放完成后,它会使用NSNotification
发出警报。
注册通知:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(itemDidFinishPlaying:) name:AVPlayerItemDidPlayToEndTimeNotification object:playerItem];
完成后调用的方法:
-(void)itemDidFinishPlaying:(NSNotification *) notification {
// Will be called when AVPlayer finishes playing playerItem
}
答案 1 :(得分:6)
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)
}
}
答案 2 :(得分:0)
这就是我做到的。
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(movieFinishedCallback:) name:AVPlayerItemDidPlayToEndTimeNotification object:player.currentItem];
- (void)movieFinishedCallback:(NSNotification*)aNotification
{
// [self dismissViewControllerAnimated:YES completion:Nil];
}