简体   繁体   中英

iOS - Can't use AVQueuePlayer initwithitems()

I want to create an array of AVPlayerItem objects from another array, by using the line in loop. 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. 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.

Even when calling InitWithItems:items[index*] , nothing happens.

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:

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

Adding the above line before you enter your loop and AVQueuePlayer should work.

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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