循环播放视频 +AVQueuePlayer

播放视频怎么循环播放secondVideoItem,在视频播放之后添加这个10-15次,只要实现secondVideoItem就行了。

NSString *secondVideoPath = [[NSBundle mainBundle] pathForResource:@"video1" ofType:@"mp4"];
    NSString *firstVideoPath = [[NSBundle mainBundle] pathForResource:@"video2" ofType:@"mp4"];


    AVPlayerItem *firstVideoItem = [AVPlayerItem playerItemWithURL:[NSURL fileURLWithPath:firstVideoPath]];
    AVPlayerItem *secondVideoItem = [AVPlayerItem playerItemWithURL:[NSURL fileURLWithPath:secondVideoPath]];

   AVQueuePlayer* queuePlayer = [AVQueuePlayer queuePlayerWithItems:[NSArray arrayWithObjects:firstVideoItem, secondVideoItem,nil]];

    AVPlayerLayer *layer = [AVPlayerLayer playerLayerWithPlayer:queuePlayer];
    avPlayer.actionAtItemEnd = AVPlayerItemStatusReadyToPlay;
    layer.frame = CGRectMake(0, 0, 1024, 768);

    [self.view.layer addSublayer:layer];

    [queuePlayer play];

ChatGPT尝试为您解答,仅供参考
您可以使用AVPlayer的actionAtItemEnd属性来实现视频循环播放。您可以将该属性设置为AVPlayerActionAtItemEndAdvance,然后将secondVideoItem添加到AVPlayer的队列中,这样当第一个视频播放完毕时,secondVideoItem就会播放。下面是一个示例:

AVPlayer *player = [AVPlayer playerWithPlayerItem:firstVideoItem];
AVPlayerLayer *layer = [AVPlayerLayer playerLayerWithPlayer:player];
layer.frame = CGRectMake(0, 0, 1024, 768);
[self.view.layer addSublayer:layer];
player.actionAtItemEnd = AVPlayerActionAtItemEndAdvance;

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(itemDidFinishPlaying:) name:AVPlayerItemDidPlayToEndTimeNotification object:player.currentItem];
[player play];

- (void)itemDidFinishPlaying:(NSNotification *)notification {
  AVPlayerItem *item = [notification object];
  [item seekToTime:kCMTimeZero];
  [player insertItem:secondVideoItem afterItem:nil];
}

在这个示例中,我们使用了AVPlayer的AVPlayerItemDidPlayToEndTimeNotification通知来监听视频播放完毕的事件,并在视频播放完毕后将secondVideoItem添加到AVPlayer的队列中。这样就可以循环播放secondVideoItem了。


如果您想要在视频播放之后添加secondVideoItem多次,您可以使用一个循环或计数器来跟踪播放的次数,并在达到指定的次数后停止添加secondVideoItem。