ios - 如何在 AVPlayer 中强制横向模式?

标签 ios swift video orientation avplayer

我正在寻找一种方法来强制 AVplayer(视频)仅在横向模式下显示自身(左侧的主页按钮)。这可能吗?

编辑:尝试添加 playerViewController.supportedInterfaceOrientations = .landscapeLeft

收到错误信息 “无法分配给属性:‘supportedInterfaceOrientations’是一个只获取属性”

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

               }
            }
        }
    }
}

编辑:尝试添加 AVPlayerViewController 的新子类<​​/p>

import UIKit
import AVFoundation
import AVKit

class LandscapeVideoControllerViewController: AVPlayerViewController {

  override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
    return .landscapeLeft
  }

}

但收到错误消息“方法没有覆盖其父类(super class)中的任何方法”

最佳答案

如果您使用 AVPlayerViewController 来呈现视频,您应该能够使用 View Controller 的继承支持的 iterface orientations 属性,如下所示:

let vc = AVPlayerViewController()
vc.supportedInterfaceOrientations = .landscapeRight
// other configs and code to present VC

我还没有机会对此进行测试,但它应该可以工作,试一试,如果您有任何问题,请发布您的代码,我会查看并更新答案。

编辑:Taytee 指出这是一个只读属性。

您应该能够通过扩展 AVPlayerController 并覆盖支持的方向属性来实现它,在单独的文件中创建此类:

import AVKit

class LandscapeAVPlayerController: AVPlayerViewController {

    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return .landscapeLeft
    }
}

然后在上面的代码中,您将更改使用的类

var playerViewController = LandscapeAVPlayerController()

注意 1:在上面的代码中,您创建了两个 AVPlayerController 实例,您不需要第二个实例。

注意 2:许多您通常会在 Swift 中覆盖的配置相关方法现在在 Swift 3 中是属性,因此您不是覆盖方法,而是覆盖属性的“getter”

关于ios - 如何在 AVPlayer 中强制横向模式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39519938/

相关文章:

ios - viewForAnnotation 返回 nil

ios - 通过 CloudFront 进行私有(private) HTTP 直播

ios - 如何在 Objective c 中显示来自 Web View 的弹出窗口

ios - xcode中的XML数据解析并存储

ios - 如果我插入或删除行,UITableView 部分标题会消失

objective-c - 如何与消息、邮件、打印和复制共享

ios - Xcode 显示输入任何变量时出现 "Expected declaration"错误

ios - 将可选字符串解包为 Int 并使其成为非可选的

video - openCV cvCreateVideoWriter 总是返回 null

javascript - 构建 phonegap/cordova iOS p2p 视频聊天