尝试无缝循环AVPlayer(userCapturedVideo)时出现问题 [英] Problem when attempting to loop AVPlayer (userCapturedVideo) seamlessly

查看:67
本文介绍了尝试无缝循环AVPlayer(userCapturedVideo)时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找有关如何正确完成此操作的时间.我在此处此处中使用了最佳答案,但是尝试完成此操作对我而言,录制的视频永远不会甚至开始循环.第一帧出现了,但没有播放视频,因此我想知道是怎么回事.

I have been looking around for a while on how to correctly accomplish this. I have looked here and here. And have used the top answer here, to try and accomplish this however for me the recorded video does not ever even begin to loop. The first frame shows up but does not play the video, thus I am wondering what's wrong.

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()
            }
        }
    }
}

以上方法中使用的功能如下.

The function used in the above method is bellow.

    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()
}

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

I have attempted the following in the function above:

        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()
            }
        })

    }

但是,这只会导致视频在播放时甚至无法播放.

This however just causes the video to not even play when it comes up.

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

The goal is to prevent the "gap" you see when the video is looped

更新:

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

Ok so actually the variable num should be outside of the function which then would make the NS code run but that ends up freezing the view like it did before.

更新2:

到目前为止,我还无法找到解决方案.但是可以肯定的是

I have not been able to find a solution to this as of yet. But what is for certain is that

`            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()
                }
            })

` 导致视频甚至无法循环播放.为什么会发生这种情况?

` Causes the video to not even loop. Any reason as to why this happens?

推荐答案

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

I did the same thing a few years back using this piece of code:

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")
}

希望这会指向您想要的功能

Hope this points you to your desired functionality

这篇关于尝试无缝循环AVPlayer(userCapturedVideo)时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆