AVPlayerViewController 可以通过 Storyboard 自定义还是必须通过添加子视图来完成?

Can AVPlayerViewController be customized via Storyboard or must it be done via adding subviews?

我正在使用 AVPlayerViewController/AVPlayer 播放 mp3。我希望能够在播放器开始时添加一个图像和一个 textView。我已经能够通过添加子视图来做到这一点,但我无法让 textView 滚动。我在想也许我可以只使用 Storyboard 并将 AVPlayerViewController 拖到其中并像常规 viewController 那样对其进行自定义...但这似乎不可能,因为 Xcode 崩溃了几次我已经试过了。 是否有可能(或建议)尝试通过 Storyboard 添加 imageView and/or textView,或者我是否必须使用子视图?

如果它是子视图,我怎样才能让我的 textView 滚动(我有另一个 post 问这个问题)? 我目前拥有的代码(没有Storyboard 上的 AVPlayerViewController)是:

let url = URL(string:mystream)
    let player = AVPlayer(url: url!)
    
    let controller = AVPlayerViewController()
    
    controller.player = player
    controller.view.frame = self.view.frame

    controller.contentOverlayView!.backgroundColor = UIColor(red: 148/255, green: 148/255, blue: 184/255, alpha: 1)
  
    //.. view
    var myView = UIView(frame: CGRect(x: 0, y: 0, width: view.bounds.width, height: view.bounds.height))

    //.. imageView
    var imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 325, height: 325))
    imageView.image = myImage
    imageView.contentMode = .scaleAspectFit
    imageView.translatesAutoresizingMaskIntoConstraints = false

    //.. textView
    var myTextView = UITextView(frame: CGRect(x: 0, y: 0, width: 350, height: 600))
    //myTextView.text = currentList
    myTextView.text = "Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda. "
    myTextView.font = UIFont(name: "Chalkboard SE", size: 18)
    myTextView.textColor = UIColor.yellow
    myTextView.backgroundColor = UIColor.gray
    
    //..
    myTextView.contentMode = .scaleAspectFit
    
    myTextView.isScrollEnabled = true
    myTextView.isUserInteractionEnabled = true
    myTextView.isEditable = true
    myTextView.isSelectable = true
    myTextView.bounces = true
    myTextView.showsVerticalScrollIndicator = true
    myTextView.showsHorizontalScrollIndicator = true
    myTextView.contentSize = CGSize(width: 700, height: 700)
    
    myTextView.translatesAutoresizingMaskIntoConstraints = false
    
    myView.addSubview(imageView)
    myView.addSubview(myTextView)
    
    controller.contentOverlayView?.addSubview(myView)

    //.. @@@@@@@@@
    imageView.topAnchor.constraint(equalTo: myView.safeAreaLayoutGuide.topAnchor, constant: 50).isActive = true
    imageView.centerXAnchor.constraint(equalTo: myView.centerXAnchor, constant: 0).isActive = true
    imageView.widthAnchor.constraint(equalToConstant: 325).isActive = true
    imageView.heightAnchor.constraint(equalToConstant: 325).isActive = true

    myTextView.topAnchor.constraint(equalTo: imageView.bottomAnchor, constant: 15).isActive = true
    myTextView.centerXAnchor.constraint(equalTo: myView.centerXAnchor, constant: 0).isActive = true
    myTextView.widthAnchor.constraint(equalToConstant: 325).isActive = true
    myTextView.heightAnchor.constraint(equalToConstant: 200).isActive = true
    
    //.. @@@@@@@@@
    
    present(controller, animated: true) {
            player.play()
    }

这是当前播放器显示的...我需要能够滚动文本...

AVPlayerViewController presented...

目前的简短回答是,无法在故事板中工作和自定义 AVPlayerViewController

您可能在故事板中添加了一个 UIViewController 并将 UIViewController 的 class 设置为 AVPlayerViewController 并且 AVPlayerViewController 的实现可能是不准备处理这个。

目前,您无法将 AVPlayerViewController 添加到故事板并移动控件或添加子视图。

最接近在故事板中使用视图的操作是子class AVPlayerViewController

class CustomAVPlayerController: AVPlayerViewController
{
 // your implementation
}

如果您实现了 required init?(coder: NSCoder)

,您就可以在情节提要中使用它

但是,在 Apple recommends against subclassing AVPlayerViewController.

中谨慎走这条路

Overview

A player view controller makes it simple to add media playback capabilities to your app that match the styling and features of the native system players. Using this object also means that your app automatically adopts the new features and styling of future operating system releases.

我会说只有当你必须做一些基本的工作时才使用它,比如检测方向变化,为视图添加一些颜色等,而不是乱用子视图和控件。

如果您只使用 MP3,您可以使用 AVPlayer 在正常 UIViewController 而不是 AVPlayerViewController 中播放您的音频,这就是我为其中一个所做的apps I developed - Reliable Radio

这是我能够实现的 UI 的示例:

上述方法的优点是您可以根据需要自定义视图,该方法的缺点是您必须开发所有控件,例如 play, forward, rewind, volume, scrubber etc

这是一个

所以这是你愿意妥协的选择:

  1. 没有多少UI自定义,但所有控件都是免费的 - AVPlayerViewController
  2. 完整 UI 控件,但开发您自己的控件 - AVPlayer 包含在自定义 UIViewController

我也会很快回答你关于 UITextView 滚动问题的第二个问题,解决方案介于上述 1 和 2 之间

幸运的是,如今在故事板上使用 AVPlayerViewController 变得轻而易举...

您肯定会想要使用容器视图。

(与任何容器视图一样,只需添加一行代码即可获得 segue ... EXPLANATION 如果需要。)

(请注意,与任何时候单击以添加容器视图一样,Xcode 恰好使新控制器成为普通的 UIViewController。只需单击将其更改为 AVPlayerViewController。) (EXPLANATION 如果需要。)

然后用代码操作AVPVC就很简单了,

    var ezPlayer: AVPlayerViewController!
    ...

    ezPlayer.showsPlaybackControls = true
    ezPlayer.player = AVPlayer(url: u)

等等等等