9

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

有没有办法删除这个默认的活动指示器视图AVPlayerViewController

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

具有自定义控件和默认活动指示器的播放器

4

2 回答 2

3

我做了一个扩展AVPlayerViewController,暴露了内部活动指标。给你,带着所有 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 { [$0] + $0.subviews }
            .flatMap { [$0] + $0.subviews }
            .flatMap { [$0] + $0.subviews }
        return nestedSubviews.filter { $0 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
于 2017-02-26T20:32:25.417 回答
1

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

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
    }
}
于 2016-07-12T14:23:30.223 回答