无法观察AVPlayerItem的@“status”键 [英] Can’t observe AVPlayerItem for @“status” key
问题描述
我正在尝试从网址播放流,但这是一个问题。 ObserveValueForKeyPath:ofObject:更改:上下文不执行。据我了解,这不取决于streamURL,它是否正确。它必须从AVPlayerItemStatusUnknown将状态更改为AVPlayerItemStatusReadyToPlay或AVPlayerItemStatusFailed。但是,我在浏览器中检查了该URL,它运行正常。所以有什么问题?我观看了WWDC视频,它说,如果你有音频流,请在 AVAsset之前使用AVPLayerItem 。
I’m trying to play stream from URL, but here is an issue. ObserveValueForKeyPath:ofObject:change:context just doesn’t execute. As I understand, it is not depends on streamURL, nether it is correct or not. It must change status to AVPlayerItemStatusReadyToPlay or AVPlayerItemStatusFailed from AVPlayerItemStatusUnknown. But nonetheless I checked that URL in browser, and it is working fine. So, what’s the problem? I watched WWDC video about it, and it says, if you have audio stream, use AVPLayerItem before AVAsset.
这是我的代码:
- (void) prepare
{
NSURL * trackStreamURL = [NSURL URLWithString:streamURL];
NSLog(@"STREAM URL: %@",trackStreamURL);
_playerItem = [AVPlayerItem playerItemWithURL:trackStreamURL];
[_playerItem addObserver:self forKeyPath:@"status" options:0 context:nil];
}
- (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
NSLog(@"KEY PATH : %@", keyPath);
NSLog(@"CHANGE : %@", change);
}
提前致谢!
推荐答案
我一直在 AVPlayer
上观察到状态
属性而不是 AVPlayerItem
因为你需要一个玩家来玩玩家物品。事实上,我甚至从未查看过玩家项目的状态属性。
I've always observed the status
property on the AVPlayer
instead of the AVPlayerItem
since you need a player to play the player item anyway. In fact, I had never even looked at the status property of the player item.
玩家项目的状态可能永远不会改变,因为没有玩家准备好播放。因此,使用玩家项目创建一个玩家并观察玩家的状态(或者可能,仍然是玩家项目的状态)。
The status of the player item probably never changes because there is no player that is ready to play it. So, Create a player with the player item and observe the status of the player (or possibly, still the status of the player item).
AVPlayer *player = [AVPlayer playerWithPlayerItem:playerItem];
这篇关于无法观察AVPlayerItem的@“status”键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!