使用AVPlayer-Swift无缝循环播放视频 [英] Seamless looping Video using AVPlayer- Swift

查看:335
本文介绍了使用AVPlayer-Swift无缝循环播放视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用AVPlayer使用循环在后台播放本地视频,并且视频可以正常播放,但是在完成视频播放后,需要暂停才能循环播放视频.我尝试了很多方法,也看到了很多关于堆栈溢出的帖子,但是我找不到合适的解决方案.我正在使用Swift3.

I am using AVPlayer to play local video in background using loop and video is playing fine but after finishing video it takes pause to play video in loop. I have tried many methods and also seen many post on stack overflow but i failed to find appropriate solution. I am using Swift3.

代码在这里:

var videoplayer :AVPlayer = AVPlayer()

override func viewDidLoad() {
super.viewDidLoad()

let path = Bundle.main.path(forResource: "background4", ofType: "mp4")
        videoplayer = AVPlayer(url: URL(fileURLWithPath: path!))
        videoplayer.volume = 0     
        videoplayer.actionAtItemEnd = AVPlayerActionAtItemEnd.none;

        let playerLayer = AVPlayerLayer(player: videoplayer)
        playerLayer.frame = self.view.frame
        playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill

        if (videoplayer.rate != 0) {
            print("playing videoplayer")
            self.blurBgImage.isHidden = true
        }

        playerLayer.zPosition = -1
        videoplayer.rate = 0
        videoplayer.play()
        self.blurBgImage.layer.addSublayer(playerLayer)

 NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: videoplayer.currentItem, queue: nil, using: { (_) in
        DispatchQueue.main.async {

             let t1 = CMTimeMake(5, 100)
            self.videoplayer.seek(to: t1)
            self.videoplayer.play()
        }
    })
}

我也尝试过AVPlayerLooper. 代码是:

I have also tried AVPlayerLooper. Code is :

 var playerLooper: NSObject?
var playerLayer:AVPlayerLayer!
var queuePlayer: AVQueuePlayer?

 func playVideo(_ filmName: String){
       if let path = Bundle.main.path(forResource: filmName, ofType: "mp4")        let url =  URL(fileURLWithPath: path)
         if #available(tvOS 10.0, *) {
          let playerItem = AVPlayerItem(url: url as URL)
            self.videoplayer = AVQueuePlayer(items: [playerItem])
            self.playerLayer = AVPlayerLayer(player: self.videoplayer)
            self.playerLooper = AVPlayerLooper(player: self.videoplayer as! AVQueuePlayer, templateItem: playerItem)
            self.blurBgImage.layer.addSublayer(playerLayer!)
            self.playerLayer?.frame = self.view.frame
            self.videoplayer.volume = 10
            self.videoplayer.play()
        } else {
            videoplayer = AVPlayer(url: url)
            videoplayer.play()
            loopVideo(videoplayer)
        }
}
}

无缝循环应该怎么做?预先感谢.

What should i do for seamless looping? Thanks in Advance.

推荐答案

仅供参考,此处提供了示例代码: https://developer.apple.com/library/content/samplecode/avloopplayer/Introduction/Intro.html

FYI there is sample code here: https://developer.apple.com/library/content/samplecode/avloopplayer/Introduction/Intro.html

@matt删除的答案对我来说(在设备/模拟器上)对iOS 10+设备而言正常工作:

@matt's deleted answer works fine for me (on device / simulator) for iOS 10+ devices:

使用AVPlayerLooper.这就是它的目的.

Use AVPlayerLooper. That is exactly what it is for.

https://developer.apple.com/reference/avfoundation/avplayerlooper

基本上,它为您实现了AVQueuePlayer,并不断更新 排队,这样它就永远不会结束.

Basically it implements AVQueuePlayer for you, constantly updating the queue so that it never ends.

它无缝循环,没有任何白色/黑色斑点.

It seamlessly loops without any white/black blip.

例如

private var looper: AVPlayerLooper?

...

let queuePlayer = AVQueuePlayer(playerItem: item)
looper = AVPlayerLooper(player: queuePlayer, templateItem: item)
videoPlayerLayer.player = queuePlayer

如果您最终在可重复使用的单元格(例如UICollectionView)中执行此操作,请确保在单元格重复使用之前禁用循环,否则会导致一些难以理解的崩溃:

If you end up doing this in a reusable cell (e.g. UICollectionView), then make sure you disable looping before cell re-use or you'll get some obscure crashes:

looper?.disableLooping()

这篇关于使用AVPlayer-Swift无缝循环播放视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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