AVPlayer ReplaceCurrentItemWithPlayerItem 不适用于 iOS 4.3.3+
我有一个正在使用 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 中,此属性包含实际值。这个方法永远不会被再次调用。所以本质上,替换似乎总是失败。
这看起来像是一个错误,但也许我做错了什么。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我在 iOS 5(包括 5.0.1)中遇到了这个问题。它曾经在 iOS 4.x 上运行良好。
有两种方法可以解决此问题:每次需要交换曲目时,使用所需的
AVPlayerItem
释放并重新创建AVPlayer
。或者,只需在主线程上调用replaceCurrentItemWithPlayerItem:
即可。我尝试了这两种选择,效果都很好。
致谢:Apple 开发者论坛
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 desiredAVPlayerItem
s each time you need to swap tracks. Or, simply callreplaceCurrentItemWithPlayerItem:
on the main thread.I tried both options and they worked fine.
Credits to: Apple Developer Forums
我也遇到过类似的问题。您可能从 来自 Apple 示例的 AVPlayerDemoPlaybackViewController像我一样编写代码。也许
currentItem
为nil
的问题是因为它尚未加载或准备好播放(我的问题是我无法获取新AVPlayerItem< 的持续时间) /代码>)。
您可以尝试在观察到
currentItem
的状态为ReadyToPlay
时开始播放。我不知道这是否适合你,我没有在低于或高于 4.3.4 的 iPad 上尝试过这个,所以我想我很快就会遇到麻烦。
I've been experiencing similar problems. You probably got started from AVPlayerDemoPlaybackViewController from Apple sample code like me. Maybe the problem why
currentItem
isnil
is because it's not loaded yet or ready for playback (my problem was I couldn't get the duration of the newAVPlayerItem
).You could try starting the playback when observed status of the
currentItem
isReadyToPlay
.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.