3

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

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

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

下面是我播放视频序列的代码。我怎样才能实现我的 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];
4

3 回答 3

1

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

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

}
于 2013-03-12T10:39:17.760 回答
0

我能找到的最好方法是这个

观察 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];
}
于 2015-02-18T12:29:52.180 回答
0

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

于 2016-03-07T17:01:17.797 回答