如何使用 AVPlayerLooper 循环播放视频 [英] How to loop video with AVPlayerLooper

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

问题描述

我尝试使用 AVPlayerLooper 在 TV OS 应用程序中循环播放视频,因为这应该会在再次播放视频时消除暂停/打嗝.我观看了 WWDC2016 视频 https://developer.apple.com/videos/play/wwdc2016/503/ 并尝试实现代码,但它不会循环.我有一个 PlayerViewController 继承了 AVPlayerViewController.我把代码让视频循环.如果我有以下代码,它什么也不显示.如果我将第二行更改为 self.queuePlayer = AVQueuePlayer(playerItem:playerItem),它只会播放一次.

I try to loop a video in a TV OS app with the AVPlayerLooper because this should get rid of the pause/hicup when playing the video again. I watched the WWDC2016 video https://developer.apple.com/videos/play/wwdc2016/503/ and try to implement the code but it doesn't loop. I have one PlayerViewController which inherits AVPlayerViewController. I put the code to let the video loop. If I have the following code, it shows nothing. If I change the second line to self.queuePlayer = AVQueuePlayer(playerItem:playerItem), it only plays once.

  let playerItem = AVPlayerItem(url: url as URL)
  self.queuePlayer = AVQueuePlayer()   //I declared this as a variable in the view controller
  self.playerLayer = AVPlayerLayer(player: self.queuePlayer) //I declared this as a variable in the view controller
  let playerLooper = AVPlayerLooper(player:  self.queuePlayer!, templateItem: playerItem)
  self.view.layer.addSublayer(self.playerLayer!)
  self.playerLayer?.frame = self.view.frame
  self.queuePlayer?.play()

你们有没有人用最新的AVPlayerLooper成功播放循环视频?

Have any of you succeeded in playing looped video with the latest AVPlayerLooper?

推荐答案

我自己解决了这个问题.

I fixed the problem myself.

playerLooper 必须是类中的成员变量,否则它不起作用,因为调用方法后局部变量消失了.所以我把这一行放在类的开头来声明它.我没有将其声明为 AVPlayerLooper,因为这仅适用于 tvos10.0 和更新版本.我希望我的班级能够适应 tvos9.0.这是我的工作代码.

The playerLooper must be a member variable in the class otherwise it doesn't work because a local variable is gone after the method has been called. So I put this line at the beginning of the class to declare it. I didn't declare it as an AVPlayerLooper because this is only for tvos10.0 and newer versions. I want my class to be adaptive to tvos9.0. This is my working code.

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


func playVideo(_ filmName: String){
    if let path = Bundle.main.path(forResource: filmName, ofType: "mov") {
        let url =  URL(fileURLWithPath: path)

        if #available(tvOS 10.0, *) {

            // Use a new player looper with the queue player and template item
            let playerItem = AVPlayerItem(url: url as URL)
            self.player = AVQueuePlayer(items: [playerItem])
            self.playerLayer = AVPlayerLayer(player: self.player)
            self.playerLooper = AVPlayerLooper(player: self.player! as! AVQueuePlayer, templateItem: playerItem)
            self.view.layer.addSublayer(self.playerLayer!)
            self.playerLayer?.frame = self.view.frame
            self.player?.play()


        } else {
            // Fallback on earlier versions, this solution has hicup at end
            player = AVPlayer(url: url)
            player?.play()
            loopVideo(player!)
        }

    }
}

func loopVideo(_ videoPlayer: AVPlayer) {
    NotificationCenter.default.addObserver(forName: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: nil, queue: nil) { notification in
        if(!self.isStopped){

            videoPlayer.seek(to: kCMTimeZero)
            videoPlayer.play()

        }
    }
}

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

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