AVPlayer:当状态为 ReadyToPlay 且出现第一帧时延迟

AVPlayer: delay when status is ReadyToPlay and first frame appearing

我们将 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")
    }

}