Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can’t observe AVPlayerItem for @“status” key

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!

like image 467
Alfred Zien Avatar asked Mar 04 '14 17:03

Alfred Zien


1 Answers

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];
like image 142
David Rönnqvist Avatar answered Oct 24 '22 23:10

David Rönnqvist