我有一个使用AVPlayer构建的音频播放器。AVPlayer replaceCurrentItemWithPlayerItem无法在iOS 4.3.3+上工作
目前,我把player
实例周围,当我需要换轨(无论是从手动选择或轨道已经到达末端),我创建了一个新的AVPlayerItem
和我打电话replaceCurrentItemWithPlayerItem
新项目。
根据文档,replaceCurrentItemWithPlayerItem
是一个异步操作,所以我也观察了播放器的关键路径当前项目。当被调用时,我会告诉我的玩家玩。
下面是相关代码:
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:asset];
[playerItem addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:CHStreamingAudioPlayer_PlayerItemStatusChangedContext];
if (!_player) {
_player = [[AVPlayer alloc] initWithPlayerItem:playerItem];
[_player addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:CHStreamingAudioPlayer_PlayerStatusChangedContext];
[_player addObserver:self forKeyPath:@"currentItem" options:NSKeyValueObservingOptionNew context:CHStreamingAudioPlayer_PlayerCurrentItemChangedContext];
} else {
[_player replaceCurrentItemWithPlayerItem:playerItem];
}
这里是键值观察回调:
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if (context == CHStreamingAudioPlayer_PlayerCurrentItemChangedContext) {
NSLog(@"Player's current item changed! Change dictionary: %@", change);
if (_player.currentItem) {
[self play]; //<---- doesn't get called
}
} else {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}
在iOS 4.3.3(和iOS 5)我的重点观察方法被调用,但_player.currentItem
总是nil
。在4.2.1和4.3.2上,该属性包含实际值。这种方法不会再被调用。所以从本质上来说,替换似乎总是失败。
这看起来像一个错误,但也许我做错了什么。
Dunja解答了您的问题吗? –