我需要连续播放一些音频,因此AVQueuePlayer
用于此目的。我正在将音频加载为:
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];
现在的问题是,由于我是通过Async
(参见else
上面代码中的条件)从 URL 加载资产,所以我的音频文件的顺序AVQueuePlayer
与_messagesArray
例如:
_messagesArray = audioURL1 , audioURL2, audioURL3
然后由于异步,无论哪个先加载,都会AVQueuePlayer
先进入
AVQueuePlayer = audio2 (from URL2) , audio1 (from URL1) , audio3 (from URL3)
请提出一个解决方案来维持我的播放器中的顺序。
谢谢。