【问题标题】:Problem when attempting to loop AVPlayer (userCapturedVideo) seamlessly尝试无缝循环 AVPlayer (userCapturedVideo) 时出现问题
【发布时间】:2018-12-11 05:00:10
【问题描述】:

我一直在寻找有关如何正确完成此任务的方法。我看过herehere。并使用最佳答案here 来尝试完成此操作,但对我来说,录制的视频甚至都没有开始循环播放。第一帧显示但不播放视频,因此我想知道出了什么问题。

func fileOutput(_ output: AVCaptureFileOutput, didFinishRecordingTo outputFileURL: URL, from connections: [AVCaptureConnection], error: Error?) {
    if (error != nil) {
        print("Error recording movie11: \(error!.localizedDescription)")
    } else {
        isSettingThumbnail = false

        let videoRecorded = outputURL! as URL
        playRecordedVideo(video: videoRecorded)

        if !captureSession.isRunning {
            DispatchQueue.global(qos: .background).async {
                self.startRunningCaptureSession()
            }
        }
    }
}

上述方法中使用的函数如下。

    func playRecordedVideo(video: URL) {
    thumbImage = nil
    playerQueue = AVQueuePlayer(playerItem: AVPlayerItem(url: video))

    playerLayer = AVPlayerLayer(player: playerQueue)
    playerLayer.frame = (camBaseLayer?.bounds)!
    playerLayer?.layoutIfNeeded()
    playerLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill
    playerLayer.isHidden = false

    camBaseLayer?.layer.insertSublayer(playerLayer, above: previewLayer)

    playerItem1 = AVPlayerItem(url: video)

    playerLooper = AVPlayerLooper(player: playerQueue, templateItem: playerItem1)
    self.playerQueue?.play()
}

我在上面的函数中尝试了以下操作:

        var num = 0
    if num == 0 {
        self.playerQueue?.play()
        num+=1
    } else {
        NotificationCenter.default.addObserver(forName: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: self.playerQueue.currentItem, queue: nil, using: { (_) in
            DispatchQueue.main.async {
                self.playerQueue.seek(to: CMTime.zero)
                self.playerQueue.play()
            }
        })

    }

然而,这只会导致视频在出现时甚至无法播放。

目标是防止视频循环播放时出现“间隙”

更新

好的,所以实际上变量 num 应该在函数之外,这会使 NS 代码运行,但最终会像以前一样冻结视图。

更新 2:

到目前为止,我还没有找到解决方案。但可以肯定的是

`            NotificationCenter.default.addObserver(forName: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: self.playerQueue.currentItem, queue: nil, using: { (_) in
                DispatchQueue.main.async {
                    self.playerQueue.seek(to: CMTime.zero)
                    self.playerQueue.play()
                }
            })

` 导致视频甚至不循环播放。发生这种情况的任何原因?

【问题讨论】:

    标签: ios iphone swift avfoundation avplayer


    【解决方案1】:

    几年前我用这段代码做过同样的事情:

    var player: AVPlayer!
    var playerLayer: AVPlayerLayer!
    
    private func playVideo(name: String) {
        guard let path = Bundle.main.path(forResource: name, ofType:"mp4") else { return }
        player = AVPlayer(url: NSURL(fileURLWithPath: path) as URL)
        playerLayer = AVPlayerLayer(player: player)
        self.playerLayer.frame = SOME_BOUNDS
        self.player!.play()
        NotificationCenter.default.addObserver(self, selector: #selector(playerDidFinishPlaying(note:)), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: player.currentItem)
    }
    
    @objc func playerDidFinishPlaying(note: NSNotification) {
        print("Video Finished")
        self.playerLayer.removeFromSuperlayer()
        playVideo(name: "SOME_NAME")
    }
    

    希望这可以为您提供所需的功能

    【讨论】:

    • 从我的实现来看,它仍然有昙花一现
    • 但我认为这只是因为我们的资产时间不同。所以我想它可能对其他人有用
    猜你喜欢
    • 2021-03-16
    • 2017-06-25
    • 1970-01-01
    • 1970-01-01
    • 2022-01-12
    • 1970-01-01
    • 2021-08-30
    • 2023-03-07
    • 1970-01-01
    相关资源
    最近更新 更多