简体   繁体   English

iOS 8.4 特定:AVPlayer 不能同时播放视频和音频并且没有错误

[英]iOS 8.4 specific: AVPlayer not playing both video & audio and no errors

EDIT: Tested in 8.3 simulator too, same issue.编辑:也在 8.3 模拟器中测试过,同样的问题。

I have an app which works perfectly fine in iOS 9.0 onwards (all versions).我有一个应用程序,它在 iOS 9.0 及更高版本(所有版本)中运行良好。 However specific to iOS 8.4, the AVPlayer doesn't play anything.然而,对于 iOS 8.4,AVPlayer 不播放任何内容。 No audio & video.没有音频和视频。

Happens on both iPad and iPhone.在 iPad 和 iPhone 上都会发生。

I have added observer for status and rate key path and as per the logger, those method do get called as if the avplayer is playing.我为状态和速率键路径添加了观察者,并且根据记录器,这些方法确实被调用,就好像 avplayer 正在播放一样。 However in the actual device and simulator both - there is no video and audio.但是在实际设备和模拟器中 - 没有视频和音频。

I have checked the avplayer's error property too and it's null throughout.我也检查了 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);
        }
    }
}

Output:输出:

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;
}

I fixed the issue - though I am not sure why this was happening in iOS 8 but not in iOS 9.我解决了这个问题 - 虽然我不确定为什么这会发生在 iOS 8 而不是在 iOS 9 中。

The completion loadValuesAsynchronouslyForKeys was not returning to main thread.完成loadValuesAsynchronouslyForKeys没有返回到主线程。 I checked this by putting [NSThread isMainThread] and noticed it was false.我通过放置[NSThread isMainThread]检查它并注意到它是错误的。

I fixed it by switching to main thread before doing " replaceCurrentItemWithPlayerItem ".我通过在执行“ 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];
            });
        }];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM