在我的视图控制器上,我添加了一个AVPlayerViewController作为子控制器。
它能够播放来自网络的音频和视频文件。但是,我想在按下暂停按钮或后退按钮时捕获视频/音频的进度。我不确定要订阅什么事件或调用什么函数,也不能在文档中找到任何函数。有人可以展示一个例子吗?
以下是我在视图控制器上的内容
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let mediaURL = URL(string:fileMediaUrl!)
if let murl=mediaURL {
self.avPlayer=AVPlayer(url:murl)
self.avPlayerViewController.player=self.avPlayer
}
self.addChildViewController(avPlayerViewController)
self.view.addSubview(avPlayerViewController.view)
avPlayerViewController.view.frame=self.view.frame
//this is of when media has finished playing
NotificationCenter.default.addObserver(self, selector:#selector(self.playerDidFinishPlaying(note:)),name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: avPlayer?.currentItem)
avPlayer?.play()
}
答案 0 :(得分:1)
您应该接受rate
。当rate
为0.0
时,表示玩家已停止(无论出于何种原因)。
avPlayer?.addObserver(self, forKeyPath: "rate", options: NSKeyValueObservingOptions(rawValue: 0), context: nil)