bluetooth99 2013-03-13 06:07 采纳率: 0%
浏览 6298

循环播放视频 +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];
  • 写回答

1条回答

  • ChatGPTᴼᴾᴱᴺᴬᴵ 2022-12-19 22:12
    关注

    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。

    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?