AVQueuePlayer:循环队列中的最后一项(并消除打嗝)或循环播放video的最后几秒

我正在使用AVQueuePlayer按顺序播放video(虽然我在播放之间打嗝)。 现在我想:

1)循环该序列中的最后一项(而不是完整项)。

2)这样做,我也想消除打嗝。

下面是我播放video序列的代码。 我怎么能实现我的2个目标?

[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长video并循环播放它的最后几秒。 也许我也可以这样避免打嗝。 这种方法是否可以实现? 如果是这样,如何调整下面的代码(播放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]; 

以下是循环播放video的解决方案: –

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

通过编辑回答: - 上面的答案播放相同的文件,但这对我有用。 它在循环中播放最后添加的文件:

 - (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中的每个播放项目。

 queuePlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone; for(AVPlayerItem *item in items) { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(nextVideo:) name:AVPlayerItemDidPlayToEndTimeNotification object:item ]; } 

在每个nextVideo上再次插入currentItem以将其排队以进行回放。 确保为每个项目寻求零。 在advanceToNextItem之后,AVQueuePlayer将从队列中删除currentItem。 我们只是重新插入。

 -(void) nextVideo:(NSNotification*)notif { AVPlayerItem *currItem = notif.userInfo[@"object"]; [currItem seekToTime:kCMTimeZero]; [queuePlayer advanceToNextItem]; [queuePlayer insertItem:currItem afterItem:nil]; } 

我发现这样做的唯一方法实际上是可靠的,就是采用Apple的Using AVQueuePlayer来演示循环播放样本。 您需要添加自己的代码来播放初始video,然后循环播放另一个video,但该方法比我尝试过的任何其他方法都更好。