使用AVPlayer播放iOS流不起作用

时间:2016-11-22 13:58:28

标签: ios objective-c avplayer avplayeritem

我用AVPlayer播放视频流。

只有单击“播放”按钮2-5次才能播放某些流。 (抱歉,我无法提供链接,并且不确定此流中的特殊内容,但可能仅对完整的高清视频流有问题)

我喜欢这样:

字段:

AVPlayerItem *playerItem;
AVPlayer *player;
AVPlayerViewController *playerViewController;
viewDidLoad中的

player = [[AVPlayer alloc] init];
playerViewController = [[AVPlayerViewController alloc] init];
playerViewController.player = player;

点击“播放”时:

NSURL *videoURL = [NSURL URLWithString:strUrl];
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:videoURL options:nil];
playerItem = [AVPlayerItem playerItemWithAsset:asset];

[playerItem addObserver:self
    forKeyPath:@"status"
    options: NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew
    context:nil];  

[player replaceCurrentItemWithPlayerItem:playerItem];

观察:

NSLog(@"status changed");
if (((AVPlayerItem *)object).status == AVPlayerStatusReadyToPlay)
{
    NSLog(@"ready to play");
    [playerItem removeObserver:self forKeyPath:@"status"];
    [self presentViewController:playerViewController animated:YES completion:nil];
    [player play];
}
else if (((AVPlayerItem *)object).status == AVPlayerStatusFailed)
{
    NSLog(@"failed to ready");
}
else if (((AVPlayerItem *)object).status == AVPlayerStatusUnknown)
{
    NSLog(@"unknown");
}

首先总是“未知”,但在问题情况下没有消息(但大多数流正常播放,我看到“准备播放”消息)

我试图观察player.status但是当它开始播放ReadyToPlay时,我只看到第一帧和无限消息“正在加载”在屏幕顶部。

0 个答案:

没有答案