I am trying to load the next item in AVQueuePlayer
's que once the previous has finished loading for seamless playback(http streaming). I've been going off of Apples example - "AVFoundationQueuePlayer-iOS".
From what I have seen from running Apples example on my phone, when I add the first 'HTTP Live Stream' item it will load, and once it finishes, I add another and that item will also load. When looking at the code, all Apple does is [self.player insertItem:item afterItem:nil];
each time the user adds an item.
With my queuePlayer
I add the first item, wait until it finishes loading and then add another with [self.player insertItem:item afterItem:nil];
BUT this does not cause the new item to load.
Both cases are done without playing the videos. Just adding them to the que.
I load each AVURLAsset
asynchronously and when they are completed:
if (firstItem) {
// Is the first item. Add to player que
[self.player insertItem:item afterItem:nil];
}else{
// Add the loaded items to an array
NSDictionary *dic = [[NSDictionary alloc]initWithObjectsAndKeys:item, @"item", date, @"date", nil];
[itemArray addObject:dic];
}
I observe the keyPath "player.currentItem.loadedTimeRanges" of the first item and add the next item from the itemArray
once the first item has loaded.
if(smartValue == duration) {
if (itemArray.count != 0) {
NSDictionary *dic = [itemArray objectAtIndex:0];
AVPlayerItem *item = (AVPlayerItem *) [dic valueForKey:@"item"];
// Should cause the item to load ??
[self.player insertItem:item afterItem:nil];
[itemArray removeObjectAtIndex:0];
}
}
Now reading around, I've seen answers that AVQueuePlayer
will not load the next item until the player is about to finish playing the previous item. But how does Apples example load the next item.
Apples Method to add an item
for (NSString *loadedAssetTitle in self.loadedAssets.allKeys) {
AVAsset *loadedAsset = self.loadedAssets[loadedAssetTitle];
AAPLPlayerViewController __weak *weakSelf = self;
[alertController addAction:[UIAlertAction actionWithTitle:loadedAssetTitle style:UIAlertActionStyleDefault handler:
^(UIAlertAction *action){
NSArray *oldItemsArray = [weakSelf.player items];
AVPlayerItem *newPlayerItem = [AVPlayerItem playerItemWithAsset:loadedAsset];
[weakSelf.player insertItem:newPlayerItem afterItem:nil];
[weakSelf queueDidChangeFromArray:oldItemsArray toArray:[self.player items]];
}]];
}