从 AVPlayerViewController 中删除缓冲 activity 指示器

Remove buffering activity indicator from AVPlayerViewController

我正在使用 AVPlayerViewController 和自定义播放控件实现 iOS 视频播放器(即 showsPlaybackControls 属性 定义为 NO) .这似乎在大多数情况下都能正常工作,我看到的唯一问题是我也想对播放器使用自定义 activity 指示器,但似乎 AVPlayerViewController 显示默认 activity 指标,同时在某些点缓冲视频。

有没有办法从 AVPlayerViewController 中删除此默认 activity 指标视图?

图像显示了我所描述的内容,底部的控件是自定义的并覆盖在播放器的顶部,但 activity 指示器不是。

我也搜索了这个解决方案,我设法实现它的方法是在我开始播放视频后隐藏视频播放器视图控制器视图,当视频准备好播放时我再次显示它。

private func playVideo() {

    videoPlayer?.play()

    self.addLoader()

    videoPlayerController.view.hidden = true

    videoPlayer?.addObserver(self, forKeyPath: "status", options: NSKeyValueObservingOptions.New, context: nil)
}

public override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {

    if (object?.isEqual(videoPlayer) == true && keyPath == "status") {

        self.removeLoader()
        videoPlayerController.view.hidden = false
    }
}

我对 AVPlayerViewController 进行了扩展,公开了内部 activity 指标。给你,所有 Swift 3 性感:

import AVKit

extension AVPlayerViewController {
    /// Activity indicator contained nested inside the controller's view.
    var activityIndicator: UIActivityIndicatorView? {
        // Indicator is extracted by traversing the subviews of the controller's `view` property.
        // `AVPlayerViewController`'s view contains a private `AVLoadingIndicatorView` that
        // holds an instance of `UIActivityIndicatorView` as a subview.
        let nestedSubviews: [UIView] = view.subviews
            .flatMap { [[=10=]] + [=10=].subviews }
            .flatMap { [[=10=]] + [=10=].subviews }
            .flatMap { [[=10=]] + [=10=].subviews }
        return nestedSubviews.filter { [=10=] is UIActivityIndicatorView }.first as? UIActivityIndicatorView
    }

    /// Indicating whether the built-in activity indicator is hidden or not.
    var isActivityIndicatorHidden: Bool {
        set {
            activityIndicator?.alpha = newValue ? 0 : 1
        }
        get {
            return activityIndicator?.alpha == 0
        }
    }
}

有了这个,您可以轻松设置 UIActivityIndicatorView 的样式或将它们全部隐藏起来,例如:

playerViewController.isActivityIndicatorHidden = true