swift 如何使用AVPlayerLooper循环视频

6ie5vjzr  于 2023-05-16  发布在  Swift
关注(0)|答案(5)|浏览(149)

我试着用AVPlayerLooper在电视操作系统应用程序中循环播放视频,因为这应该可以消除再次播放视频时的暂停/打嗝。我看了WWDC2016视频https://developer.apple.com/videos/play/wwdc2016/503/并尝试实现代码,但它不循环。我有一个PlayerViewController,它继承了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成功地播放了循环视频吗?

sy5wg1nm

sy5wg1nm1#

我自己解决了这个问题。
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()

        }
    }
}
btqmn9zl

btqmn9zl2#

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];
}
06odsfpq

06odsfpq3#

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)
5sxhfpxr

5sxhfpxr4#

您的实现的问题是您在函数级别声明AVPlayerLayer,而它应该在类级别声明。

ctzwtxfj

ctzwtxfj5#

我创建了AVQueuePlayer的子类

class AVLooperPlayer: AVQueuePlayer {
    private var looper: AVPlayerLooper!

    convenience override init(url: URL) {
        let playerItem = AVPlayerItem(url: url)
        self.init(playerItem: playerItem)
        looper = AVPlayerLooper(player: self, templateItem: playerItem)
    }
}

Apple Docs:通过向播放器循环器传递AVQueuePlayer和模板AVPlayerItem的引用,可以创建一个播放器循环器,循环器会自动管理此内容的循环播放
用途

let avPlayer = LooperPlayer(url: url)

相关问题