【问题标题】:AVPlayer replaceCurrentItemWithPlayerItem not working on iOS 4.3.3+AVPlayer replaceCurrentItemWithPlayerItem 在 iOS 4.3.3+ 上不起作用
【发布时间】:2011-09-14 17:03:48
【问题描述】:

我有一个正在使用 AVPlayer 构建的音频播放器。

目前,我保留 player 实例,当我需要交换曲目时(无论是手动选择还是曲目已到达终点),我创建一个新的 AVPlayerItem 并使用新的调用 replaceCurrentItemWithPlayerItem项目。

根据文档,replaceCurrentItemWithPlayerItem 是一个异步操作,所以我也观察了播放器的 currentItem 键路径。当它被调用时,我告诉我的玩家玩。

以下是相关代码:

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 下面的回答解决了您的问题吗?

标签: objective-c ios avplayer


【解决方案1】:

我在使用 iOS 5(包括 5.0.1)时遇到了这个问题。它曾经在 iOS 4.x 上运行良好。

有两种方法可以解决此问题,每次需要交换曲目时,使用所需的 AVPlayerItems 释放并重新创建 AVPlayer。或者,只需在主线程上调用replaceCurrentItemWithPlayerItem:

我尝试了这两个选项,它们都运行良好。

致谢:Apple Developer Forums

【讨论】:

    【解决方案2】:

    我也遇到过类似的问题。你可能和我一样是从AVPlayerDemoPlaybackViewController from Apple sample code 开始的。也许currentItemnil 的问题是因为它尚未加载或准备好播放(我的问题是我无法获得新AVPlayerItem 的持续时间)。

    当观察到currentItem 的状态为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;
        }
    

    我不知道这是否适合你,我没有在低于或高于 4.3.4 的 iPad 上尝试这个,所以我想我很快就会遇到并发症。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-09-27
      • 2014-04-22
      • 1970-01-01
      • 1970-01-01
      • 2011-11-26
      • 1970-01-01
      • 2016-07-03
      • 2012-05-29
      相关资源
      最近更新 更多