iOS - Can't use AVQueuePlayer initwithitems()

402 views Asked by At

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.

1

There are 1 answers

0
Unheilig On BEST ANSWER

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.