我的问题是,当连接状况很差时,活动指示器开始动画直到显示视频的第一帧,然后消失以为是视频正在播放,但是视频停止在加载的第一帧上播放,直到整个视频加载然后继续播放,如何在视频卡在帧和缓冲中时显示活动指示器,然后播放直到下一个加载的帧?
注释:
当互联网连接关闭时,它可以正常工作,播放视频直到显示已加载的帧和活动指示器,然后当打开我们继续播放的视频并隐藏活动指示器时,
在可以正常连接互联网的情况下可以正常工作
使用关键值的替代observevalue删除并显示指标 “ currentItem.loadedTimeRanges” /“ currentItem.playbackBufferEmpty”
我制作了一个带有avplayer的uiview类
import UIKit
import AVKit
import AVFoundation
class videoplaying: UIView {
override static var layerClass: AnyClass {
return AVPlayerLayer.self;
}
var playerlayer: AVPlayerLayer{
return layer as! AVPlayerLayer;
}
var player: AVPlayer?{
get{
return playerlayer.player
}
set {
playerlayer.player = newValue
}
}
var playetitem: AVPlayerItem?
}
我在uicollectioncell中为该课程分配了一个uivew(使用情节提要) avplayer开始播放并在uicollectioncell中按播放时添加观察值
@IBAction func play(_ sender: Any) {
activityindicator.isHidden = false
activityindicator.startAnimating()
self.butttoonheight.isHidden = true
self.postimage.isHidden = true
let url2 = URL(string: "https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")
let avplayer = AVPlayer(url: url2! )
let playeritem = AVPlayerItem(url: url2!)
//videoss is class of type uiview
videoss.playetitem = playeritem
videoss.playerlayer.player = avplayer
videoss.player?.play()
videoss.player?.addObserver(self, forKeyPath: "currentItem.loadedTimeRanges", options: .new, context: nil)
videoss.player?.addObserver(self, forKeyPath: "rate", options: .new
, context: nil)
videoss.player?.addObserver(self, forKeyPath: "currentItem.playbackBufferEmpty", options: .new, context: nil)
playying.isHidden = false
}
//observing when video is playing
//playpause button to play or pause video while bad network is present video is stuck on first frame and playorpause is not changing while pressed
@IBAction func playorpause(_ sender: Any) {
if videoss.player?.timeControlStatus == AVPlayerTimeControlStatus.paused{
videoss.player?.play()
playying.setImage(UIImage(named: "pas50"), for: .normal)
}
if videoss.player?.timeControlStatus == AVPlayerTimeControlStatus.playing{
videoss.player?.pause()
playying.setImage(UIImage(named: "p24"), for: .normal)
}
}
override public func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if keyPath == "rate"{
print(videoss.player?.rate)
if videoss.player?.rate == 0.0 {
print("dawdaopwdaopwdipo")
}
}
if keyPath == "currentItem.loadedTimeRanges"{
print("its is working")
activityindicator.stopAnimating()
activityindicator.isHidden = true
}
if keyPath == "currentItem.playbackBufferEmpty"
{
activityindicator.startAnimating()
activityindicator.isHidden = false
print("pkawdawdawd")
}
}
答案 0 :(得分:0)
我通过添加一个计时器来解决此问题,该计时器每运行0.3秒,每次运行0.3秒,它会检查视频的当前时间(如果当前时间等于以前的时间),则显示视频未播放活动指示器;如果未播放,则显示视频活动指示器被隐藏,还需要检查用户是否暂停了视频,优点是您还可以在相同的功能中获得当前时间。 我还尝试查看视频的播放速度,但是在互联网速度缓慢的情况下,该播放速度总是可以播放的,但实际上并非如此,