AVQueuePlayer: Loop last item in a queue (and eliminate hiccup) or loop last seconds of a video

大城市里の小女人 提交于 2019-12-23 16:25:21

问题


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

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

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

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

回答1:


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

}



回答2:


best way I could find was this

observe for each playeritem in AVQueuePlayer.

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

on each nextVideo insert the currentItem again to queue it for playback. make sure to seek to zero for each item. after advanceToNextItem the AVQueuePlayer will remove the currentItem from queue. we just reinsert.

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



回答3:


The only way I've found to do this that is actually reliable is by adopting Apple's Using AVQueuePlayer to demonstrate loop playback sample. You need to add your own code to play an initial video and then loop a different one, but the method works better than any other I've tried.



来源:https://stackoverflow.com/questions/14765990/avqueueplayer-loop-last-item-in-a-queue-and-eliminate-hiccup-or-loop-last-sec

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!