0

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

- (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 永远不会加载?我怎样才能让它加载?

4

1 回答 1

4

这是一个远程资产(它在大型互联网上的某个地方)。在您尝试播放之前,它没有有意义的状态;此时,我们开始加载资产并探索其性质。您还没有尝试播放它,所以状态只是在 Unknown 处。

为什么 AVPlayerItem 永远不会加载?我怎样才能让它加载?

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

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

  • 您的第一个条件从不测试状态是否为 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

At first I see "Not known" in the console, but as soon as I try to play the asset, it changes to "Ready to play".

于 2016-06-22T03:30:17.893 回答