gpt4 book ai didi

ios - 检测 AVPlayerViewController 是否正在播放视频或缓冲并向播放器添加覆盖

转载 作者:搜寻专家 更新时间:2023-10-31 19:33:38 27 4
gpt4 key购买 nike

  1. 我必须检测视频是处于播放模式还是缓冲模式。我正在从 URL 加载视频。我已经尝试了下面的代码,我可以在视频开始播放后进行跟踪,但不能在它处于缓冲状态时进行跟踪。
  2. 另外,我想在我的播放器中添加叠加 View 。我试图在 AVPlayer 中添加叠加层,但是在全屏模式下叠加层消失了。

需要一些建议。提前致谢。下面是我的代码:

    let playerAV = AVPlayerViewController()
var player = AVPlayer()
player = AVPlayer(URL: url)
print(url)
playerAV.player = player
playerAV.view.frame = CGRectMake(0, 0, self.videoView.frame.width, self.videoView.frame.height)
self.addChildViewController(playerAV)
self.videoView.addSubview(playerAV.view)
playerAV.didMoveToParentViewController(self)
playerAV.player?.play()
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ChannelDetailViewController.notificationObserver(_:)), name:AVPlayerItemDidPlayToEndTimeNotification , object: player.currentItem)
_ = UIDevice.beginGeneratingDeviceOrientationNotifications
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ChannelDetailViewController.deviceOrientationDidChange(_:)) , name:
UIDeviceOrientationDidChangeNotification, object: nil)
player.addObserver(self, forKeyPath: "rate", options: NSKeyValueObservingOptions.New, context: nil)
player.addPeriodicTimeObserverForInterval(CMTime(value: 1, timescale: 3), queue: dispatch_get_main_queue()) { [weak self] time in
self?.handlePlayerStatus(time)
}

func handlePlayerStatus(time: CMTime) {
if player.status == .ReadyToPlay {
// buffering is finished, the player is ready to play
print("playing")
}
if player.status == .Unknown{
print("Buffering")
}
}

override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
if keyPath == "rate" {
if let rate = change?[NSKeyValueChangeNewKey] as? Float {
if player.currentItem!.status == AVPlayerItemStatus.ReadyToPlay{
if rate != 0 && player.error == nil {

print("normal playback")
}
else{
print("playback stopped")
}

}else if player.currentItem?.status == AVPlayerItemStatus.Unknown{
print("test")
}
}
}
print("you are here")
}

check here for project

最佳答案

  1. 检查我的答案:https://stackoverflow.com/a/38867386/5109911 ,这向您展示了如何检查播放器是否正在加载缓冲区,要检查它是否准备好播放,您必须同时检查 player.currentItem.statusplayer.status

  2. 要向 AVPlayer 添加叠加层,我建议在 AVPlayer 层上方使用 UIView

关于ios - 检测 AVPlayerViewController 是否正在播放视频或缓冲并向播放器添加覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38746062/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com