Can’t observe AVPlayerItem for @“status” key

倖福魔咒の 提交于 2019-12-07 06:19:50

问题


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.

Here is my code:

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

Thanks in advance!


回答1:


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


来源:https://stackoverflow.com/questions/22178444/can-t-observe-avplayeritem-for-status-key

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!