I need to play some audios in a row, and therefore using AVQueuePlayer
for that purpose. I'm loading audios in it as :
for (NSInteger i = row ; i<_messagesArray.count ; i++)
{
_urlString = ((PFFile*)(_messagesArray[i][@"messageFile"])).url;
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:[NSURL URLWithString:_urlString] options:nil];
NSArray *keys = @[@"playable"];
AVPlayerItem *item = [AVPlayerItem playerItemWithAsset:asset];
// Subscribe to the AVPlayerItem's DidPlayToEndTime notification.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(itemDidFinishPlaying:) name:AVPlayerItemDidPlayToEndTimeNotification object:item];
if (_queuePlayer == nil)
{
_queuePlayer = [[AVQueuePlayer alloc] initWithPlayerItem:item];
}
else
{ /*This loads Async*/
[asset loadValuesAsynchronouslyForKeys:keys completionHandler:^() {
[_queuePlayer insertItem:item afterItem:nil];
}];
}
}
[_queuePlayer play];
Now the problem is that since I'm loading the assets from URL via Async
(See else
condition in the code above), the order of audio files in my AVQueuePlayer
is not same as the URLs in the _messagesArray
e.g :
_messagesArray = audioURL1 , audioURL2, audioURL3
then due to async, whichever loads first, goes into AVQueuePlayer
first
AVQueuePlayer = audio2 (from URL2) , audio1 (from URL1) , audio3 (from URL3)
Please suggest a solution to maintain the order in my player.
Thanks.