![](/img/trans.png)
[英]Can I use replaceCurrentItemWithPlayerItem with AVQueuePlayer?
[英]iOS - Can't use AVQueuePlayer initwithitems()
我想通过使用循环行从另一个数组创建一个AVPlayerItem
对象数组。 它适用于单个项目,但不适用于整个数组。
MPMediaQuery *albumQuery = [MPMediaQuery albumsQuery];
MPMediaPropertyPredicate *albumPredicate = [MPMediaPropertyPredicate predicateWithValue:@"Out Of Exile" forProperty:MPMediaItemPropertyAlbumTitle];
[albumQuery addFilterPredicate:albumPredicate];
NSArray *songs = [albumQuery items];
NSMutableArray <AVPlayerItem*> *items;
NSUInteger i = 0;
while(i < [songs count]){
AVPlayerItem *playerItem = [[AVPlayerItem alloc] initWithURL:[[songs objectAtIndex:i] valueForProperty:MPMediaItemPropertyAssetURL]];
[items addObject:playerItem];
i++;
}
AVPlayerItem *item = [[AVPlayerItem alloc] initWithURL:[[songs objectAtIndex:index] valueForProperty:MPMediaItemPropertyAssetURL]]; //manually created item which works
_player = [[AVQueuePlayer alloc] initWithItems:items];
[_player play];
因此,我的问题是我无法正确地使用and数组初始化AVQueuePlayer
,这使我发疯。 如果我使用InitWithPlayerItem
对其进行初始化并添加创建的项目,则它可以运行(播放),但不适用于items数组中的任何对象。
即使调用InitWithItems:items[index*]
,也不会发生任何事情。
您的代码有效; 之所以将items
添加到AVQueuePlayer
却不起作用, AVQueuePlayer
一个接一个地手动添加一个item
,是因为您忘记了初始化数组的原因:
NSMutableArray <AVPlayerItem*> *items = [[NSMutableArray alloc] init];
在进入循环之前添加以上行, AVQueuePlayer
应该可以工作。
希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.