我用一个视图开始了一个新项目,并将这段代码添加到视图控制器中:
- (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 永远不会加载?我怎样才能让它加载?