[英]Remove buffering activity indicator from AVPlayerViewController
I'm implementing an iOS video player using AVPlayerViewController
with custom playback controls (ie, the showsPlaybackControls
property is defined as NO
). 我正在使用带有自定义播放控件的AVPlayerViewController
实现iOS视频播放器(即, showsPlaybackControls
属性定义为NO
)。 This seems to work properly in most cases, the only issue I'm seeing is that I would like to use a custom activity indicator with the player as well, but it seems that AVPlayerViewController
shows a default activity indicator while buffering the video at some points. 这似乎在大多数情况下都能正常工作,我看到的唯一问题是我想在播放器中使用自定义活动指示器,但似乎AVPlayerViewController
在某些点缓冲视频时显示默认活动指示器。
Is there a way to remove this default activity indicator view from AVPlayerViewController
? 有没有办法从AVPlayerViewController
删除此默认活动指示器视图?
The image shows what I'm describing, the controls at the bottom are custom and overlaid on top of the player, but the activity indicator is not. 图像显示我正在描述的内容,底部的控件是自定义的并覆盖在播放器顶部,但活动指示器不是。
I made an extension of AVPlayerViewController
that exposes the internal activity indicator. 我做了一个AVPlayerViewController
的扩展,它公开了内部活动指示器。 Here you go, with all the Swift 3 sexiness: 在这里,所有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
}
}
}
With this, you can either easily style the UIActivityIndicatorView
or just hide it all together, eg: 有了这个,您可以轻松地设置UIActivityIndicatorView
样式,或者只是将它们全部隐藏起来,例如:
playerViewController.isActivityIndicatorHidden = true
I also searched for this solution and the way I managed to make it is hide video player view controllers view once I started playing video and when video is ready to play I show it again. 我也搜索了这个解决方案,我设法制作它的方式是隐藏视频播放器视图控制器视图一旦我开始播放视频和视频准备播放我再次显示它。
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
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.