简体   繁体   English

iOS-无法使用AVQueuePlayer initwithitems()

[英]iOS - Can't use AVQueuePlayer initwithitems()

I want to create an array of AVPlayerItem objects from another array, by using the line in loop. 我想通过使用循环行从另一个数组创建一个AVPlayerItem对象数组。 It works for a single item but not for entire array. 它适用于单个项目,但不适用于整个数组。

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];

So, my problem is that I cannot properly initialize AVQueuePlayer with and array and it's driving me mad. 因此,我的问题是我无法正确地使用and数组初始化AVQueuePlayer ,这使我发疯。 If I initialize it with InitWithPlayerItem and add the item which I created it works (plays), but it doesn't work with any object from the items array. 如果我使用InitWithPlayerItem对其进行初始化并添加创建的项目,则它可以运行(播放),但不适用于items数组中的任何对象。

Even when calling InitWithItems:items[index*] , nothing happens. 即使调用InitWithItems:items[index*] ,也不会发生任何事情。

Your code works; 您的代码有效; the reason why adding items to AVQueuePlayer does not work whilst adding manually one single item after another works is because you have forgotten to initialize your array: 之所以将items添加到AVQueuePlayer却不起作用, AVQueuePlayer一个接一个地手动添加一个item ,是因为您忘记了初始化数组的原因:

NSMutableArray <AVPlayerItem*> *items = [[NSMutableArray alloc] init];

Adding the above line before you enter your loop and AVQueuePlayer should work. 在进入循环之前添加以上行, AVQueuePlayer应该可以工作。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM