如何使用AVPlayerLooper循环播放视频

时间:2016-08-31 20:17:29

标签: ios swift video tvos

我尝试使用AVPlayerLooper在带有WWDC2016的电视操作系统应用中循环播放视频,因为这样可以在再次播放视频时摆脱暂停/打嗝。我观看了PlayerViewController视频https://developer.apple.com/videos/play/wwdc2016/503/并尝试实施代码,但它不会循环播放。 我有一个AVPlayerViewController继承了self.queuePlayer = AVQueuePlayer(playerItem:playerItem)。我把代码放到视频循环中。如果我有以下代码,它什么也没显示。如果我将第二行更改为 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

您是否有人成功使用最新的{{1}}?

播放循环播放的视频

3 个答案:

答案 0 :(得分:22)

我自己解决了这个问题。

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

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

        }
    }
}

答案 1 :(得分:4)

Objective-C版本:

@implementation ViewController
{
    // As explained in accepted answer, the playerLooper must be a
    // member (I put everything as member for simplicity):
    AVPlayerItem *_playerItem;
    AVQueuePlayer *_player;
    AVPlayerLooper *_playerLooper;
    AVPlayerLayer *_playerLayer;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    // ...

    NSString *videoFile = [[NSBundle mainBundle] pathForResource:@"example" ofType:@"mov"];
    NSURL *videoURL = [NSURL fileURLWithPath:videoFile];

    _playerItem = [AVPlayerItem playerItemWithURL:videoURL];
    _player = [AVQueuePlayer queuePlayerWithItems:@[_playerItem]];
    _playerLooper = [AVPlayerLooper playerLooperWithPlayer:_player templateItem:_playerItem];
    _playerLayer = [AVPlayerLayer playerLayerWithPlayer:_player];
    _playerLayer.frame = self.view.bounds;
    [self.view.layer addSublayer:_playerLayer];
    [_player play];
}

答案 2 :(得分:1)

Swift 5自定义类,用于播放循环播放的视频而没有任何故障

import Foundation
import AVKit

class VideoPlayerLooped {

    public var videoPlayer:AVQueuePlayer?
    public var videoPlayerLayer:AVPlayerLayer?
    var playerLooper: NSObject?
    var queuePlayer: AVQueuePlayer?

    func playVideo(fileName:String, inView:UIView){

        if let path = Bundle.main.path(forResource: fileName, ofType: "mov") {

            let url = URL(fileURLWithPath: path)
            let playerItem = AVPlayerItem(url: url as URL)

            videoPlayer = AVQueuePlayer(items: [playerItem])
            playerLooper = AVPlayerLooper(player: videoPlayer!, templateItem: playerItem)

            videoPlayerLayer = AVPlayerLayer(player: videoPlayer)
            videoPlayerLayer!.frame = inView.bounds
            videoPlayerLayer!.videoGravity = AVLayerVideoGravity.resizeAspectFill

            inView.layer.addSublayer(videoPlayerLayer!)

            videoPlayer?.play()
        }
    }

    func remove() {
        videoPlayerLayer?.removeFromSuperlayer()

    }
}

用法:

let videoHolder = UIView()
videoHolder.frame = CGRect(x: 0, y: 0, width: 200, height: 200)

var myVideoPlayerLooped = VideoPlayerLooped()
videoPlayerLooped.playVideo(video: "myLocalMovFile", inView: videoHolder)