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
toAVQueuePlayer
does not work whilst adding manually one singleitem
after another works is because you have forgotten to initialize your array:Adding the above line before you enter your loop and
AVQueuePlayer
should work.Hope this helps.