AVPlayerItem 从未准备好播放

AVPlayerItem never ready to play

我开始了一个只有一个视图的新项目,并将这段代码添加到视图控制器中:

- (void)viewDidLoad {
    [super viewDidLoad];

    NSURL *videoURL;
    videoURL = [NSURL URLWithString:@"https://s3.amazonaws.com/xxxxxx.mp4"];
    AVURLAsset *asset = [AVURLAsset URLAssetWithURL:videoURL options:nil];
    AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:asset];

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

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object
                        change:(NSDictionary *)change context:(void *)context {

    if (context == @"AVPlayerStatus") {
        NSLog(@"AVPlayerStatus changed");

        if ([object isKindOfClass:[AVPlayerItem class]] && [keyPath isEqualToString:@"status"]) {
            NSLog(@"Ready to play");
        }
        else if (((AVPlayerItem *)object).status == AVPlayerStatusFailed) {
            NSLog(@"Failed to Ready");
        }
        else if (((AVPlayerItem *)object).status == AVPlayerStatusUnknown) {
            NSLog(@"Not known");
        }
    }
}

输出为:

2016-06-21 19:57:56.911 VideoTest[5740:1334235] AVPlayerStatus changed
2016-06-21 19:57:56.911 VideoTest[5740:1334235] Not known

为什么 AVPlayerItem 从未加载过?我怎样才能加载它?

这是一项远程资产(它位于广阔的互联网上的某个地方)。在您尝试播放它之前,它没有任何意义;那时,我们开始加载资产并探索其性质。您还没有尝试玩它,所以状态只是未知。

Why doesn't the AVPlayerItem ever load? and how can I get it to load?

您可以通过播放资产来加载该项目。

您的代码在三个方面存在缺陷:

  • 你的第一个条件从不测试状态是否为ReadyToPlay;

  • 您从未尝试玩该资产。

  • 你用的 context 错了。

我用你的代码进行了测试,但像这样编辑,以解决所有这三个问题:

@interface ViewController ()
@property AVPlayer* player;
@end

@implementation ViewController

- (IBAction)doLoad:(id)sender {
    NSURL *videoURL;

    videoURL = [NSURL URLWithString:@"https://s3.amazonaws.com/lookvideos.mp4/t/05093dabec6c9448f7058a4a08f998155b03cc41.mp4"];
    AVURLAsset *asset = [AVURLAsset URLAssetWithURL:videoURL options:nil];
    AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:asset];

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


    self.player = [[AVPlayer alloc] initWithPlayerItem:playerItem];
    [self.player play];
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object
                        change:(NSDictionary *)change context:(void *)context {

    NSLog(@"AVPlayerStatus changed");

    if (((AVPlayerItem *)object).status == AVPlayerStatusReadyToPlay) {
        NSLog(@"Ready to play");
    }
    else if (((AVPlayerItem *)object).status == AVPlayerStatusFailed) {
        NSLog(@"Failed to Ready");
    }
    else if (((AVPlayerItem *)object).status == AVPlayerStatusUnknown) {
        NSLog(@"Not known");
    }
}

@end

起初我在控制台中看到 "Not known",但当我尝试播放资产时,它立即变为 "Ready to play"。