AVPlayer replaceCurrentItemWithPlayerItem not work

2020-07-02 10:50发布

问题:

I have an audio player that I'm building using AVPlayer.

Currently, I keep the player instance around and when I need to swap tracks (either from a manual selection or the track has reached the end) I create a new AVPlayerItem and I call replaceCurrentItemWithPlayerItem with the new item.

According to the docs, replaceCurrentItemWithPlayerItem is an asynchronous operation, so I also observe the currentItem key path of the player. When that gets called, I tell my player to play.

Here is the relevant code:

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];
}

And here is the key value observation callback:

- (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];
    }
}

On iOS 4.3.3 (and iOS 5) my key observation method is called but _player.currentItem is always nil. On 4.2.1 and 4.3.2 this property contains the actual value. This method is never called again. So in essence, replacing seems to always fail.

This seems like a bug, but perhaps I'm doing something wrong.

回答1:

I had this issue with iOS 5 (including 5.0.1). It used to work fine on iOS 4.x.

There are two ways to workaround this, release and recreate your AVPlayer with the desired AVPlayerItems each time you need to swap tracks. Or, simply call replaceCurrentItemWithPlayerItem: on the main thread.

I tried both options and they worked fine.

Credits to: Apple Developer Forums



回答2:

I've been experiencing similar problems. You probably got started from AVPlayerDemoPlaybackViewController from Apple sample code like me. Maybe the problem why currentItem is nil is because it's not loaded yet or ready for playback (my problem was I couldn't get the duration of the new AVPlayerItem).

You could try starting the playback when observed status of the currentItem is ReadyToPlay.

AVPlayerStatus status = [[change objectForKey:NSKeyValueChangeNewKey] integerValue];
    switch (status) {
        case AVPlayerStatusUnknown: {
            NSLog(@"PLAYER StatusUnknown");
        }
            break;
        case AVPlayerStatusReadyToPlay: {
            NSLog(@"PLAYER ReadyToPlay");
            [self play];
        }
            break;
        case AVPlayerStatusFailed: {
            AVPlayerItem *playerItem = (AVPlayerItem *)object;
            [self handleError: [playerItem.error localizedDescription]];
        }
            break;
    }

I don't know if this will wok for you, I didn't try this on lower or higher than 4.3.4 iPad, so I guess I'll run into complications soon.