问题描述
我想在AVPlayerViewController中播放本地视频,但没有找到完成按钮的点击事件。
I want to play local video in AVPlayerViewController but did not find click event of Done button.
我的视频能够在AVPlayerViewController中播放,但我没有找到下一个按钮,上一个按钮和完成按钮单击事件。
My video is able to play in AVPlayerViewController but I did not find next button , previous button and done button click event.
推荐答案
官方没有完成按钮点击事件,Apple的工程师说了。
Officially there is no Done Button click event, a engineer from Apple said about it here.
至于我的研究,我发现如果单击完成按钮,有一种但是非常间接的方式来获取事件。
As for my research, I found out that there is one but really indirect way to get an event if Done Button was clicked.
我发现AVPlayerViewController的唯一变量是点击完成按钮时更改 AVPlayerViewController.view.frame
。
首先view.frame出现在viewController的中心。
I found out that the only variable of AVPlayerViewController, which is changing when done button is clicked is AVPlayerViewController.view.frame
.First of all view.frame is appearing in the center of the viewController.
如果你用表示动画:true
它转到viewController的底部,然后回到中心。单击完成后,它会返回到底部。
If you present it with animated : true
it goes to the bottom of viewController and the back to the center. When done is clicked it goes back to the bottom.
如果您使用表示动画:false
将会出现只有2个更改:当你开始播放视频时,框架将位于viewController的中心,而当单击完成时,框架将位于底部。
If you present it with animated : false
there will be only 2 changes: frame will be at the center of viewController when you start to play video, and at the bottom, when Done is clicked.
因此,如果你添加观察者 AVPlayerViewController.view.frame
回调现在(PlayerViewController,动画:true)
你只会得到一个当点击完成按钮并且视频视图将不在屏幕上时,观察者的调用正确。
So if you add observer to the AVPlayerViewController.view.frame
in the callback to present(PlayerViewController, animated : true)
you'll get only one call of the observer, right when done button is clicked and video view will be out of the screen.
在我的情况下,AVPlayerViewController以模态方式呈现动画。以下代码对我有用:
In my case AVPlayerViewController was presented modally with animation. Code below worked for me:
Swift 3.0
override func viewDidLoad()
{
super.viewDidLoad()
let videoURL = NSURL(fileURLWithPath:Bundle.main.path(forResource: "MyVideo", ofType:"mp4")!)
let player = AVPlayer(url: videoURL as URL)
present(playerViewController, animated: false) { () -> Void in
player.play()
self.playerViewController.player = player
self.playerViewController.addObserver(self, forKeyPath: #keyPath(UIViewController.view.frame), options: [.old, .new], context: nil)
}}
override func observeValue(forKeyPath keyPath: String?,
of object: Any?,
change: [NSKeyValueChangeKey : Any]?,
context: UnsafeMutableRawPointer?)
{
print(self.playerViewController.view.frame)
//Player view is out of the screen and
//You can do your custom actions
}
,我发现当你单击Done时,AVPlayerViewController没有被解雇,你可以在 ParentViewController.presentedViewController
中看到它,所以你不能将观察者添加到这个属性
Also, I found out when you click Done, AVPlayerViewController is not dismissed and you can see it in ParentViewController.presentedViewController
, so you can't add observer to this property
这篇关于在AVPlayerViewController中完成按钮单击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!