我一直在查看AVPlayerItem
和AVPlayer
文档,但似乎没有回调函数可以在项目播放完成后调用。我希望能够利用某种委托回调或者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
}
[avPlayer currentItem]
。 - abhimuralidharanSwift-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];
}