我在AVQueuePlayer上遇到了removeAllItems的问题。
我使用AVQueuePlayer播放来自网络的音乐。为了播放下一首歌,在这个类中我们有方法advanceToNextItem。但是对于以前我们应该创建新的队列。
为什么我这样做:
- (void) initPlayer{
...
playlist = [NSMutableArray array];
playlistItems = [NSMutableArray array];
...
player = [AVQueuePlayer queuePlayerWithItems:nil];
player.actionAtItemEnd = AVPlayerActionAtItemEndAdvance;
...
}
- (void) play{
if ([player.items count] <= 0){
[self playAtIndex:0]; //first run fires here
}
[player play];
}
- (void)playAtIndex:(int)index
{
[player removeAllItems]; // <------at first run it work correctly
for (int i = index; i <playlistItems.count; i ++) {
AVPlayerItem* obj = [playlistItems objectAtIndex:i];
if ([player canInsertItem:obj afterItem:nil]) {
[obj seekToTime:kCMTimeZero];
[player insertItem:obj afterItem:nil];
}
}
}
- (void) addTracks:(NSArray *)tracks{
[playlist addObjectsFromArray:tracks];
for (Track *track in tracks){
AVPlayerItem *item = [AVPlayerItem playerItemWithURL:[NSURL URLWithString:track.trackURL]];
...
[playlistItems addObject:item];
}
}
然后我和玩家一起工作:
[player initPlayer];
[player addTracks:tracksList];
[player play]; // all works fine, play next, pause and so on
...
在某个地方我去追踪并调用playAtIndex:x,首先调用[player removeAllItems]并且程序崩溃。
Xcode回复我:
***由于未捕获的异常'NSRangeException'终止应用程序,原因:'*** - [__ NSArrayM objectAtIndex:]:索引2147483647超出边界[0 .. 153]'
我试图只调用[player removeAllItems]并崩溃。
哪里可能有问题?