我们将来自 URL 的 MP4 视频加载到 AVPlayer 中。当 AVPlayer 进入“Ready To Play”状态时,AVPlayer 在我们隐藏的骨架图像后面。
我们希望在隐藏骨架图像后立即看到视频的第一帧。但是,视频的第一帧会在稍有延迟后出现。指示 AVPlayer 中的视频已加载并准备就绪的状态是什么?
func prepareVideo(videoUrl: URL?, owner: UIView, autoStart: Bool = false) {
self.player = AVPlayer(url: videoUrl!)
let playerController = AVPlayerViewController()
playerController.player = player
playerController.view.layer.shouldRasterize = true
playerController.view.frame = owner.frame
playerController.view.isOpaque = true
playerController.view.backgroundColor = UIColor.clear
playerController.view.layer.borderColor = UIColor.clear.cgColor
playerController.view.layer.borderWidth = 0
playerController.showsPlaybackControls = false
playerController.updatesNowPlayingInfoCenter = false
owner.addSubview(playerController.view)
owner.sendSubview(toBack: playerController.view)
timerStart() // this starts a timer that checks the AVPlayer status
if autoStart {
playVideo()
}
}
@objc func timerStatusCheck() {
// function called by a Timer and checks the status of AVPlayer
if player!.status == AVPlayerStatus.readyToPlay {
print("ready to play")
timerStop()
if (readyToPlayHandler != nil) {
self.readyToPlayHandler!() // here we hide the skeleton image that shows while video is loading
}
} else if player!.status == AVPlayerStatus.failed {
timerStop()
MessageBox.showError("Video Failed to start")
}
}
最佳答案
当 AVPlayer 报告 rate = 1 时,它正在播放视频。但是,这并不意味着视频在 AVPlayerViewController 中可见。为此,您需要 AVPlayerViewController 的属性“isReadyForDisplay”为真。
https://developer.apple.com/documentation/avkit/avplayerviewcontroller/1615830-isreadyfordisplay
请注意 AVPlayerViewController.isReadyForDisplay 和 AVPlayer.status 都是 KVO 可观察的,这将比使用计时器响应更快。
另请注意,如果您使用 AVPlayerLayer 来显示视频(而不是 AVPlayerViewController),出于同样的原因,您需要观察 AVPlayerLayer 的“isReadyForDisplay”。
@objc func timerStatusCheck() {
// function called by a Timer and checks the status of AVPlayer
if player!.status == AVPlayerStatus.readyToPlay {
print("ready to play")
// check that controller's view is showing video frames from player
if playerController.isReadyForDisplay == false {
print("view not yet showing video")
return
}
timerStop()
if (readyToPlayHandler != nil) {
self.readyToPlayHandler!() // here we hide the skeleton image that shows while video is loading
}
} else if player!.status == AVPlayerStatus.failed {
timerStop()
MessageBox.showError("Video Failed to start")
}
}
关于ios - AV播放器 : delay when status is ReadyToPlay and first frame appearing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51348984/