问题描述
我正在寻找一种方法来强制AV播放器(视频)仅以横向模式显示自己(左侧的主页按钮)。这可能吗?
编辑:尝试添加
playerViewController.supportedInterfaceOrientations = .landscapeLeft
收到错误消息
无法分配给属性:'supportedInterfaceOrientations'是一个get-only属性
import AVKit
import AVFoundation
UIViewController {
var videoPlayer = AVPlayer()
var playerViewController = AVPlayerViewController()
覆盖func viewDidAppear(_ animated:Bool){
super.viewDidAppear(动画)
play()
}
func play(){
playerViewController.supportedInterfaceOrientations = .landscapeLeft
let moviePath = Bundle.main.path(forResource:full video,ofType:mov)
if let path = moviePath {
let url = NSURL.fileURL(withPath:path)
let videoPlayer = AVPlayer(url:url)
让playerViewController = AVPlayerViewController()
playerViewController.player = v ideoPlayer
self.present(playerViewController,animated:true){
if let validPlayer = playerViewController.player {
validPlayer.play()
playerViewController.showsPlaybackControls = false
}
}
}
}
}
编辑:尝试添加新的AVPlayerViewController子类
import UIKit
import AVFoundation
import AVKit
类LandscapeVideoControllerViewController:AVPlayerViewController {
override func supportedInterfaceOrientations() - > UIInterfaceOrientationMask {
return .landscapeLeft
}
}
但收到错误消息方法不会覆盖其超类中的任何方法
如果您正在使用 AVPlayerViewController
来呈现您应该能够使用视图控制器的继承支持的iterface orientation属性的视频,如下所示:
让vc = AVPlayerViewController()
vc.supportedInterfaceOrientations = .landscapeRight
//提供VC
的其他配置和代码
我没有机会测试这个,但它应该有效,试一试,如果您有任何问题请发布您拥有的代码,我将查看并更新答案。
编辑:Taytee指出这是一个只读属性。
您应该能够通过扩展AVPlayerController并覆盖支持的方向属性来实现它,在单独的文件中创建此类:
import AVKit
class LandscapeAVPlayerController:AVPlayerViewController {
override var supportedInterfaceOrientations:UIInterfaceOrientationMask {
return .landscapeLeft
}
}
然后在上面的代码中,你' d改变使用的类如
var playerViewController = LandscapeAVPlayerController()
注意1:在上面的代码中,你创建了两个AVPlayerController实例,你不需要第二个实例。
I'm looking for a way to force a AVplayer (Video) to only display itself in landscape mode (home button on the left). Is this possible?
EDIT: Attempted to addplayerViewController.supportedInterfaceOrientations = .landscapeLeft
Got error message"Cannot assign to property: 'supportedInterfaceOrientations' is a get-only property"
import AVKit
import AVFoundation
UIViewController {
var videoPlayer = AVPlayer()
var playerViewController = AVPlayerViewController()
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
play()
}
func play() {
playerViewController.supportedInterfaceOrientations = .landscapeLeft
let moviePath = Bundle.main.path(forResource: "full video", ofType: "mov")
if let path = moviePath {
let url = NSURL.fileURL(withPath: path)
let videoPlayer = AVPlayer(url: url)
let playerViewController = AVPlayerViewController()
playerViewController.player = videoPlayer
self.present(playerViewController, animated: true) {
if let validPlayer = playerViewController.player {
validPlayer.play()
playerViewController.showsPlaybackControls = false
}
}
}
}
}
EDIT: Attempted to add new subclass of AVPlayerViewController
import UIKit
import AVFoundation
import AVKit
class LandscapeVideoControllerViewController: AVPlayerViewController {
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return .landscapeLeft
}
}
but getting error message "Method does not override any method from its superclass"
If you are using the AVPlayerViewController
to present the video you should be able to use the inherited supported iterface orientations property of the view controller, Like so:
let vc = AVPlayerViewController()
vc.supportedInterfaceOrientations = .landscapeRight
// other configs and code to present VC
I haven't had chance to test this but it should work, give it a try and if your having any issues please post the code you have and I'll have a look and update the answer.
EDIT: Taytee pointed out that this is a read only property.
You should be be able to implement it by extending the AVPlayerController and overriding the supported orientation properties, Create this class in a seperate file:
import AVKit
class LandscapeAVPlayerController: AVPlayerViewController {
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return .landscapeLeft
}
}
and then in your code above, you'd change the class used like
var playerViewController = LandscapeAVPlayerController()
NOTE 1: In your code above you are creating TWO instances of AVPlayerController, you dont need the second one.
NOTE 2: Alot of the config related methods you would normally override in Swift are now properties in Swift 3, so instead of overriding the method, you override the 'getter' for the property
这篇关于如何在AVPlayer中强制横向模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!