![](/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.