我一直在到处寻找,但仍然找不到解决此问题的方法。我有一个AVQueuePlayer
我正在制作的广播应用程序。它AVPlayerItems
一次大约有20个排队。我将AVPlayerItemDidPlayToEndTimeNotification
观察者添加到这些项目中的每一项。通知被触发并执行代码以删除元数据和状态的关键观察者,因此它不会崩溃并前进到队列中的下一个项目。然而它不想玩。没有报错,状态已经准备好播放,AVPlayer
网址加载完美。如果我单击按钮来调用AdvanceNextitem,它可以完美运行并且也可以完美播放。现在最奇怪的是:如果我手动发布通知,通知代码可以完美运行。我将不胜感激任何帮助或输入,因为我一直在尝试一切以使其正常工作。
if (playlist != nil) {
playlistInterval = [playlist count]/4.0;
NSMutableArray *playerItems = [[NSMutableArray alloc] initWithCapacity:playlistInterval];
for(int i = 0; i < playlistInterval; i++) {
NSString *tempString = [playlist objectAtIndex:i];
//NSLog(@"%@", tempString);
NSURL *temp = [[NSURL alloc] initWithString:tempString];
AVPlayerItem *itemTemp = [AVPlayerItem playerItemWithURL:temp];
[itemTemp addObserver:self forKeyPath:@"timedMetadata" options:NSKeyValueObservingOptionNew context:nil];
[itemTemp addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:nil];
[[NSNotificationCenter defaultCenter] addObserver: self
selector: @selector(playerItemDidReachEnd:)
name: AVPlayerItemDidPlayToEndTimeNotification
object: itemTemp];
[playerItems addObject:itemTemp];
playlistCounter++;
}
qPlayer = [AVQueuePlayer queuePlayerWithItems:playerItems];
qPlayer.actionAtItemEnd = AVPlayerActionAtItemEndAdvance;
[ qPlayer addObserver:self
forKeyPath:@"rate"
options:NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew
context:rateDidChangeKVO];
[qPlayer addObserver:self forKeyPath:@"currentItem.duration"
options:0
context:durationDidChangeKVO];
return YES;
}
return NO;
}
- (IBAction)advancePlayer:(id)sender {
unsigned long size = [[qPlayer items] count];
NSLog(@"%tu",size);
if (size <= 0) {
[self initializePlayerWithItems:currentKey];
[qPlayer play];
} else {
//NSLog(@"%@",[qPlayer currentItem]);
[[qPlayer currentItem] removeObserver:self forKeyPath:@"status"];
[[qPlayer currentItem] removeObserver:self forKeyPath:@"timedMetadata"];
[qPlayer advanceToNextItem];
[qPlayer play];
}
}
- (void)playerItemDidReachEnd:(NSNotification *)notification {
[self advancePlayer:nil];
NSLog(@"IT REACHED THE END");
}
现在,如果我称这一切都可以通过按钮或其他东西完美运行:
[[NSNotificationCenter defaultCenter]
postNotificationName:@"AVPlayerItemDidPlayToEndTimeNotification"
object:[qPlayer currentItem]];