简体   繁体   中英

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

EDIT: Tested in 8.3 simulator too, same issue.

I have an app which works perfectly fine in iOS 9.0 onwards (all versions). However specific to iOS 8.4, the AVPlayer doesn't play anything. No audio & video.

Happens on both iPad and 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. 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.

- (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.

The completion loadValuesAsynchronouslyForKeys was not returning to main thread. I checked this by putting [NSThread isMainThread] and noticed it was false.

I fixed it by switching to main thread before doing " 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];
            });
        }];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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