Tôi đã tìm ra giải pháp để lặp qua tất cả các video trong hàng đợi video của mình, không chỉ một video. Trước tiên tôi khởi của tôi AVQueuePlayer
:
- (void)viewDidLoad
{
NSMutableArray *vidItems = [[NSMutableArray alloc] init];
for (int i = 0; i < 5; i++)
{
// create file name and make path
NSString *fileName = [NSString stringWithFormat:@"intro%i", i];
NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:@"mov"];
NSURL *movieUrl = [NSURL fileURLWithPath:path];
// load url as player item
AVPlayerItem *item = [AVPlayerItem playerItemWithURL:movieUrl];
// observe when this item ends
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(playerItemDidReachEnd:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:item];
// add to array
[vidItems addObject:item];
}
// initialize avqueueplayer
_moviePlayer = [AVQueuePlayer queuePlayerWithItems:vidItems];
_moviePlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone;
// create layer for viewing
AVPlayerLayer *layer = [AVPlayerLayer playerLayerWithPlayer:_moviePlayer];
layer.frame = self.view.bounds;
layer.videoGravity = AVLayerVideoGravityResizeAspectFill;
// add layer to uiview container
[_movieViewContainer.layer addSublayer:layer];
}
Khi thông báo được đăng
- (void)playerItemDidReachEnd:(NSNotification *)notification {
AVPlayerItem *p = [notification object];
// keep playing the queue
[_moviePlayer advanceToNextItem];
// if this is the last item in the queue, add the videos back in
if (_moviePlayer.items.count == 1)
{
// it'd be more efficient to make this a method being we're using it a second time
for (int i = 0; i < 5; i++)
{
NSString *fileName = [NSString stringWithFormat:@"intro%i", i];
NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:@"mov"];
NSURL *movieUrl = [NSURL fileURLWithPath:path];
AVPlayerItem *item = [AVPlayerItem playerItemWithURL:movieUrl];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(playerItemDidReachEnd:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:item];
// the difference from last time, we're adding the new item after the last item in our player to maintain the order
[_moviePlayer insertItem:item afterItem:[[_moviePlayer items] lastObject]];
}
}
}
Bạn có bị kẹt vào thời điểm này hoặc bạn chỉ cần tạo từ điểm xuất phát? – Dhruv
Tôi thực sự bây giờ làm thế nào để tạo ra nó và chơi tất cả các AVQueuePlayers, tôi bây giờ đang tìm kiếm khởi động lại máy nghe nhạc khi QVPlayerItem cuối cùng được thực hiện. – Edelweiss
'- (void) playVideoAtIndex: (NSInteger) chỉ mục { [self performSelector: @selector (setObservationInfo)]; currentIndex = index; AVPlayerItem * videoItem = [AVPlayerItem playerItemWithURL: [NSURL fileURLWithPath: [đối tượng arrVideoListAtIndex: index]]]; } ' nơi bạn cần kiểm tra, ' if (currentIndex <[arrVideoList count] -1) { currentIndex ++; } else { currentIndex = 0; } [tự phátVideoAtIndex: currentIndex]; ' – Dhruv