xcode AVPlayer Swift:如何隐藏控件并禁用横向视图?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37875659/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
AVPlayer Swift: How do I hide controls and disable landscape view?
提问by D.Bo
since this is my first post, just a few words about me: Usually I design stuff (UI primarily) but I really wanted to take a leap into programming to understand you guys better. So I decided build a small app to get going.
因为这是我的第一篇文章,所以只说几句关于我的:通常我设计东西(主要是 UI),但我真的很想跳入编程领域以更好地理解你们。所以我决定构建一个小应用程序来开始。
So I've been trying to figure this out for hours now – this is my first app project ever so I apologise for my newbyness.
所以我几个小时以来一直在努力解决这个问题——这是我有史以来的第一个应用程序项目,所以我为我的新手道歉。
All I want to do is to hide the controls of AVPlayer and disable landscape view but I just can't figure out where to put showsPlaybackControls = false
.
我想要做的就是隐藏 AVPlayer 的控件并禁用横向视图,但我就是不知道将showsPlaybackControls = false
.
import UIKit
import AVKit
import AVFoundation
class ViewController: UIViewController {
private var firstAppear = true
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
if firstAppear {
do {
try playVideo()
firstAppear = false
} catch AppError.InvalidResource(let name, let type) {
debugPrint("Could not find resource \(name).\(type)")
} catch {
debugPrint("Generic error")
}
}
}
private func playVideo() throws {
guard let path = NSBundle.mainBundle().pathForResource("video", ofType:"mp4") else {
throw AppError.InvalidResource("video", "m4v")
}
let player = AVPlayer(URL: NSURL(fileURLWithPath: path))
let playerController = AVPlayerViewController()
playerController.player = player
self.presentViewController(playerController, animated: false) {
player.play()
}
}
}
enum AppError : ErrorType {
case InvalidResource(String, String)
}
回答by EmilioPelaez
showsPlaybackControls
is a property of AVPlayerViewController
, so you can set it after you create it:
showsPlaybackControls
是 的属性AVPlayerViewController
,因此您可以在创建它后对其进行设置:
playerController.showsPlaybackControls = false
If you want to allow only landscape, you can subclass AVPlayerViewController
, override supportedInterfaceOrientations
and return only landscape, then use that class instead of AVPlayerViewController
.
如果您只想允许横向,则可以子类化AVPlayerViewController
,覆盖supportedInterfaceOrientations
并仅返回横向,然后使用该类而不是AVPlayerViewController
.
UPDATE
更新
As mentioned in the comments, if you go to the documentation for AVPlayerViewController
you'll find a warning that says:
如评论中所述,如果您查看文档,AVPlayerViewController
您会发现一条警告:
Do not subclass AVPlayerViewController. Overriding this class's methods is unsupported and results in undefined behavior.
不要子类化 AVPlayerViewController。不支持覆盖此类的方法并导致未定义的行为。
They probably don't want you to override playback-related methods that could interfere with the behavior, and I would say that overriding onlysupportedInterfaceOrientations
is safe.
他们可能不希望您覆盖可能干扰行为的与播放相关的方法,我会说仅覆盖supportedInterfaceOrientations
是安全的。
However, if you want an alternative solution, you can create your own view controller class that overrides supportedInterfaceOrientations
to return landscape only, and place the AVPlayerViewController
inside that view controller.
但是,如果您需要替代解决方案,您可以创建自己的视图控制器类,该类覆盖supportedInterfaceOrientations
以仅返回横向,并将其放置AVPlayerViewController
在该视图控制器内部。