编辑:也在8.3模拟器中测试,同样的问题。
我有一个应用程序,在iOS 9.0以上(所有版本)都可以正常运行。无论如何特定于iOS 8.4,AVPlayer都不会播放任何内容。没有音频和视频。
发生在iPad和iPhone上。
我已添加了状态和速率键路径的观察者,并且根据记录器,这些方法会被调用,就像avplayer正在播放一样。但是在实际的设备和模拟器中 - 没有视频和音频。
我也检查了avplayer的错误属性,并且它始终为空。
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object
change:(NSDictionary *)change context:(void *)context {
if (object == ((AppDelegate*)[[UIApplication sharedApplication] delegate]).avplayer) {
if ( [keyPath isEqualToString:@"status"]) {
NSLog(@"Status changed: %@, %@",change,((AppDelegate*)[[UIApplication sharedApplication] delegate]).avplayer.error.localizedDescription);
}
else if ([keyPath isEqualToString:@"rate"] && ((AppDelegate*)[[UIApplication sharedApplication] delegate]).avplayer.status == AVPlayerStatusReadyToPlay ) {
NSLog(@"Rate changed: %@",change);
}
}
}
输出:
2016-03-13 15:01:47.152 XXXXX[47910:2113657] Status changed: {
kind = 1;
new = 1;
}, (null)
2016-03-13 15:01:47.153 XXXXX[47910:2113567] Rate changed: {
kind = 1;
new = 1;
}
2016-03-13 15:01:47.160 XXXXX[47910:2113567] Rate changed: {
kind = 1;
new = 1;
}
答案 0 :(得分:2)
我修复了这个问题 - 虽然我不确定为什么会在iOS 8中发生这种情况但在iOS 9中却没有。
完成loadValuesAsynchronouslyForKeys
未返回主线程。我通过放[NSThread isMainThread]
检查了这一点,发现它是假的。
我在执行" replaceCurrentItemWithPlayerItem
"之前切换到主线程来修复它。
AVAsset *asset = [AVAsset assetWithURL:url];
[asset loadValuesAsynchronouslyForKeys:@[@"duration"] completionHandler:^{
NSLog(@"Main: %d",[NSThread isMainThread]);
dispatch_async(dispatch_get_main_queue(), ^{
AVPlayerItem *newItem = [[AVPlayerItem alloc] initWithAsset:asset];
[((AppDelegate*)[[UIApplication sharedApplication] delegate]).avplayer replaceCurrentItemWithPlayerItem:newItem];
[self addObserversToPlayer];
[((AppDelegate*)[[UIApplication sharedApplication] delegate]).avplayer play];
});
}];