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



我正在使用AVQueuePlayer按顺序播放视频(尽管我在播放之间会打嗝)。现在我想:

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

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

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

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

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中的每个playeritem。

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

在每个nextVideo上,再次插入currentItem以排队播放。确保每个项目都为零。前进到NextItem之后,AVQueuePlayer将从队列中删除currentItem。我们只是重新插入。

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

我发现唯一可靠的方法是采用Apple的Using AVQueuePlayer来演示循环播放示例。你需要添加自己的代码来播放初始视频,然后循环播放不同的视频,但这种方法比我尝试过的任何其他方法都要好。

最新更新