在我的項目中,我使用Kolada視圖(here)來呈現一堆卡片,每張卡片上都有一個視頻。有時當我加載下一個視頻時,我的AVPlayer
無法播放視頻,它只會顯示第一幀並被凍結。我使用幾個觀察者來追蹤視頻實際播放的時間,如果它可能跟上播放。如果觀察員沒有正確解除分配,觀衆是否會導致播放凍結?如果不是,還有什麼會導致AVPlayer
播放停止播放?AVPlayer不播放
更新:我注意到playbackLikelyToKeepUp
有時不會被調用,而當發生這種情況時,即使我更改了播放器項目,AVPlayer
也不會再次調用它。因此,視頻播放從不恢復。
init(frame: CGRect, mediaURL: NSURL, thumbURL: NSURL?, isLoop: Bool) {
self.mediaURL = mediaURL
self.isLoop = isLoop
let asset = AVAsset(URL: mediaURL)
let item = AVPlayerItem(asset: asset)
player = AVPlayer(playerItem: item)
player!.actionAtItemEnd = .None
super.init(frame: frame)
asset.loadValuesAsynchronouslyForKeys(["duration"], completionHandler: {() -> Void in
var error: NSError?
let keyStatus: AVKeyValueStatus = asset.statusOfValueForKey("duration", error: &error)
switch (keyStatus) {
case .Loaded:
let duration: CMTime = asset.duration
self.currentDuration = duration
self.delegate?.updateDuration()
default:
break
}
})
NSNotificationCenter.defaultCenter().addObserver(
self,
selector: #selector(MediaPlayerView.playerItemDidReachEnd(_:)),
name: AVPlayerItemDidPlayToEndTimeNotification,
object: player!.currentItem)
self.timeObserver = self.player!.addPeriodicTimeObserverForInterval(CMTimeMakeWithSeconds(1.0/30.0, Int32(NSEC_PER_SEC)), queue: nil, usingBlock: {
[weak self] (time) -> Void in
if time.value > 0 {
self?.spinnerView?.stopAnimating()
if(self?.player != nil){
if(self?.player!.rate > 0){
self?.placeholderImageView?.hidden = true
}
}
let timeInSeconds = CMTimeGetSeconds(time)
self?.delegate?.playbackReachedTime(timeInSeconds)
}
})
player!.currentItem?.addObserver(self, forKeyPath: playbackLikelyToKeepUp, options: .New, context: &observationContext)
player!.addObserver(self, forKeyPath: "rate", options: .New, context: &observationContext)
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(MediaPlayerView.playerIsTapped(_:)))
addGestureRecognizer(tapGesture)
}
是一個ViewController的初始化器? –
該avplayer作爲唯一的一個實例和一個觀察者在所有卡片視圖中跟蹤視頻結尾 –
加載下一個視頻時是否刪除了上一個視頻? – Godinall