AVPlayer Swift:如何隐藏控件并禁用横向视图? [英] AVPlayer Swift: How do I hide controls and disable landscape view?

查看:26
本文介绍了AVPlayer Swift:如何隐藏控件并禁用横向视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因为这是我的第一篇文章,所以就我简单说几句:通常​​我设计东西(主要是 UI),但我真的想跳入编程领域以更好地理解你们.所以我决定开发一个小应用程序.

所以我几个小时以来一直在努力解决这个问题——这是我有史以来的第一个应用项目,所以我为我的新手道歉.

我想要做的就是隐藏 AVPlayer 的控件并禁用横向视图,但我不知道将 showsPlaybackControls = false 放在哪里.

导入 UIKit导入 AVKit导入 AVFoundation类视图控制器:UIViewController {私有变量 firstAppear = true覆盖 func viewDidAppear(动画:布尔){super.viewDidAppear(动画)如果第一次出现{做 {试试 playVideo()第一次出现 = 假} catch AppError.InvalidResource(let name, let type) {debugPrint("找不到资源 \(name).\(type)")} 抓住 {debugPrint("一般错误")}}}私有函数 playVideo() 抛出 {守卫让路径 = NSBundle.mainBundle().pathForResource("video", ofType:"mp4") else {throw AppError.InvalidResource("video", "m4v")}让玩家 = AVPlayer(URL: NSURL(fileURLWithPath: path))让 playerController = AVPlayerViewController()playerController.player = 玩家self.presentViewController(玩家控制器,动画:假){播放器播放()}}}枚举 AppError : ErrorType {案例无效资源(字符串,字符串)}

解决方案

showsPlaybackControlsAVPlayerViewController 的一个属性,所以你可以在创建之后设置:>

playerController.showsPlaybackControls = false

如果你只想允许横向,你可以子类化 AVPlayerViewController,覆盖 supportedInterfaceOrientations 并只返回横向,然后使用该类而不是 AVPlayerViewController.

更新

正如评论中提到的,如果您转到 AVPlayerViewController 的文档,您会发现一条警告说:

<块引用>

不要继承 AVPlayerViewController.不支持覆盖此类的方法并导致未定义的行为.

他们可能不希望您覆盖可能干扰行为的与播放相关的方法,我会说覆盖 supportedInterfaceOrientations 是安全的.>

但是,如果您想要替代解决方案,您可以创建自己的视图控制器类来覆盖 supportedInterfaceOrientations 以仅返回横向,并放置 AVPlayerViewController 在该视图控制器中.

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.

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.

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)
}

解决方案

showsPlaybackControls is a property of AVPlayerViewController, so you can set it after you create it:

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.

UPDATE

As mentioned in the comments, if you go to the documentation for AVPlayerViewController you'll find a warning that says:

Do not subclass AVPlayerViewController. Overriding this class’s methods is unsupported and results in undefined behavior.

They probably don't want you to override playback-related methods that could interfere with the behavior, and I would say that overriding only supportedInterfaceOrientations is safe.

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.

这篇关于AVPlayer Swift:如何隐藏控件并禁用横向视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆