我有一個使用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解答了您的問題嗎? –