AVQueuePlayer:循环播放队列中的最后一项(并消除打ic)或循环播放视频的最后几秒钟 [英] AVQueuePlayer: Loop last item in a queue (and eliminate hiccup) or loop last seconds of a video

查看:194
本文介绍了AVQueuePlayer:循环播放队列中的最后一项(并消除打ic)或循环播放视频的最后几秒钟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用AVQueuePlayer顺序播放视频(尽管在播放之间会出现打ic). 现在我想:

I am using AVQueuePlayer to play videos in sequence (although I have hiccups between plays). Now I'd like to:

1)循环按该顺序的最后一个项目(而不是完整的项目).

1) Loop the last item in that sequence (not the full one).

2)为此,我也想消除打ic.

2) In doing so, I'd also like to eliminate hiccups.

下面是我播放视频序列的代码.我怎么能实现我的两个目标?

Below is my code to play the videos sequence. How could I achieve my 2 goals?

[super viewDidLoad];

NSString *secondVideoPath = [[NSBundle mainBundle] pathForResource:@"vid_1" ofType:@"mov"];
NSString *firstVideoPath = [[NSBundle mainBundle] pathForResource:@"vid_2" ofType:@"mov"];


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

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

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

[self.view.layer addSublayer:layer];

[queuePlayer play];

我正在考虑的另一种方法是播放1个长视频并循环播放最后几秒钟.也许我也可以这样避免打ic.这种方法可以实现吗?如果是这样,如何改编下面的代码(播放1个视频)?

Another method I'm thinking of would be the play 1 long video and loop the last few seconds of it. Perhaps I could also avoid hiccups that way. Would this approach be something achievable? If so, how to adapt the code below (that plays 1 video)?

[super viewDidLoad];
NSString *filepath = [[NSBundle mainBundle] pathForResource:@"vid_long" ofType:@"mov"];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
avPlayer = [AVPlayer playerWithURL:fileURL];

AVPlayerLayer *layer = [AVPlayerLayer playerLayerWithPlayer:avPlayer];
avPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone;
layer.frame = CGRectMake(0, 0, 1024, 768);
[self.view.layer addSublayer: layer];

[avPlayer play];

推荐答案

以下是循环播放视频的解决方案:-

Here is the solution to play the video in loop:-

queuePlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone; 
int k=0;
 [[NSNotificationCenter defaultCenter] addObserver:self
                                           selector:@selector(itemPlayEnded:)
                                               name:AVPlayerItemDidPlayToEndTimeNotification
                                             object:[queuePlayer currentItem]];

- (void)itemPlayEnded:(NSNotification *)notification 
{
 k++;
 if(k<10-15 times)
 {
  AVPlayerItem *p = [notification object];
  [p seekToTime:kCMTimeZero];
 }
}

通过修改后的答案:- 上面的答案播放相同的文件,但这对我有用. 它将循环播放最后添加的文件:

By Edited answer:- The above answer play the same files but this works for me. It play the last added file in loop:

- (void)itemPlayEnded1:(NSNotification *)notification
{
    NSString *secondVideoPath = [[NSBundle mainBundle] pathForResource:@"video2" ofType:@"mp4"];
      k=0;
    AVPlayerItem *secondVideoItem = [AVPlayerItem playerItemWithURL:[NSURL fileURLWithPath:secondVideoPath]];
    // AVPlayerItem *p = [notification object];
    NSMutableArray *currentItemArray = [NSMutableArray arrayWithObjects:secondVideoItem,nil];
    AVQueuePlayer* queuePlayer = [[AVQueuePlayer alloc] initWithItems:currentItemArray];
    [queuePlayer play];
    queuePlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone;

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

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(itemPlayEnded:)
                                                 name:AVPlayerItemDidPlayToEndTimeNotification
                                               object:[queuePlayer currentItem]];

}

这篇关于AVQueuePlayer:循环播放队列中的最后一项(并消除打ic)或循环播放视频的最后几秒钟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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