Replay items in AVQueuePlayer after last

后端 未结 4 2041
独厮守ぢ
独厮守ぢ 2021-01-05 04:29

I need to create something like an infinite loop in my AVQueuePlayer. Especially, I want to replay the whole NSArray of AVPlayerItems

4条回答
  •  借酒劲吻你
    2021-01-05 05:00

    best way to loop a sequence of videos in AVQueuePlayer.

    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.

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

提交回复
热议问题