提问人:Pangu 提问时间:5/9/2016 最后编辑:CommunityPangu 更新时间:11/12/2019 访问量:9417
如何在 Swift 中使用 AVPlayerViewController 检测全屏模式?
How to detect fullscreen mode using AVPlayerViewController in Swift?
问:
我正在尝试检测何时处于全屏模式,但我很难实现这一点。我想知道用户何时选择展开按钮进入全屏,如下所示:AVPlayerViewController
我根据以下建议添加了适当的观察者:
相应的代码:
var avWidth:CGFloat = 375
var avHeight:CGFloat = 300
override func viewDidLoad()
{
super.viewDidLoad()
let path = NSBundle.mainBundle().pathForResource("cable pressback", ofType: "mp4")
let url = NSURL.fileURLWithPath(path!)
let player = AVPlayer(URL: url)
playerViewController.player = player
playerViewController.view.frame = CGRectMake(0, 100, self.view.frame.size.width, 300)
playerViewController.view.translatesAutoresizingMaskIntoConstraints = true
view.addSubview(playerViewController.view)
self.addChildViewController(playerViewController)
[playerViewController .addObserver(self, forKeyPath:"videoBounds" , options: NSKeyValueObservingOptions.New, context: nil)]
}
override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>)
{
print("playerViewController.view.frame = \(playerViewController.view.frame)")
if keyPath == "videoBounds"
{
let rect = change!["new"]! as! NSValue
if let newrect = rect.CGRectValue() as CGRect?
{
if newrect.width > 0 || newrect.height > 0
{
if avWidth > 0 || avHeight > 0
{
if newrect.width > avWidth || newrect.height > avHeight
{
print("Full Screen")
}
else if newrect.width < avWidth || newrect.height < avHeight
{
print("Normal screen")
}
}
avWidth = newrect.width
avHeight = newrect.height
}
}
}
}
但是,它似乎从未达到代码。无论玩家是处于正常模式还是全屏模式,它都会命中。print("Full Screen")
print("Normal Screen")
谢谢!
答:
0赞
Vipin Johney
7/25/2016
#1
您的代码帮助我处理了全屏和后退之间的切换。
但是对于识别部分,我只是根据我的要求稍微更改了一下。
let rect = change!["new"] as! NSValue
if let playerRect: CGRect = rect.CGRectValue() as CGRect {
if playerRect.size == UIScreen.mainScreen().bounds.size {
print("Video in full screen")
} else {
print("Video not in full screen")
}
}
希望这会有所帮助。
评论
0赞
Vinu David Jose
11/15/2017
但全屏大小与 UIScreen.mainScreen().bounds.size 不匹配
4赞
Pangu
3/6/2017
#2
针对 Swift 3 进行了更新:
为对象添加观察者:playerViewController
playerViewController(self, forKeyPath: "videoBounds", options: NSKeyValueObservingOptions.new, context: nil)
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?)
{
if keyPath == "videoBounds"
{
if let rect = change?[.newKey] as? NSValue
{
if let newrect = rect.cgRectValue as CGRect?
{
// 200 is height of playerViewController in normal screen mode
if newrect.size.height <= 200
{
print("normal screen")
}
else
{
print("full screen")
}
}
}
}
}
0赞
Albert Renshaw
12/4/2017
#3
在 iOS11 中,如果 AVPlayer 全屏显示,您的屏幕安全区域将降至 0。尽管它可能是一个未记录的功能(因此存在潜在的错误)。我很难找到有关它的更多信息。
[UIApplication sharedApplication].keyWindow.safeAreaLayoutGuide.layoutFrame.size.height == 0?
0赞
palme
2/8/2019
#4
这是 @Pangu 答案的略微优化的 Swift 4.2 版本。它只检测变化,否则在与视频交互(如快进)时也会调用观察者。我还用键路径替换了“videoBounds”,以避免字符串,并使用窗口边界来确定它是否是全屏的。AVPlayerViewController.videoBounds
avPlayerViewController.addObserver(self, forKeyPath: #keyPath(AVPlayerViewController.videoBounds), options: [.old, .new], context: nil)
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey: Any]?, context: UnsafeMutableRawPointer?) {
if keyPath == #keyPath(AVPlayerViewController.videoBounds) {
// detect only changes
if let oldValue = change?[.oldKey] as? CGRect, oldValue != CGRect.zero, let newValue = change?[.newKey] as? CGRect {
// no need to track the initial bounds change, and only changes
if !oldValue.equalTo(CGRect.zero), !oldValue.equalTo(newValue) {
if newValue.size.height < UIScreen.main.bounds.height {
print("normal screen")
} else {
print("fullscreen")
}
}
}
}
}
10赞
Lawliet
11/12/2019
#5
从 iOS 12 开始,我们可以使用这些 AVPlayerViewControllerDelegate 委托方法:
func playerViewController(AVPlayerViewController, willBeginFullScreenPresentationWithAnimationCoordinator: UIViewControllerTransitionCoordinator)
func playerViewController(AVPlayerViewController, willEndFullScreenPresentationWithAnimationCoordinator: UIViewControllerTransitionCoordinator)
评论