在AVPlayer和其他player之间切换时,会有一个监听播放状态然后去暂停的一个动作。目前发现一个坑:iOS14系统在APP进入前台时也会触发KVO中的status值改变方法observeValueForKeyPath,会导致AVPlayer暂停。
解决办法:定义一个布尔值变量_isBackToForeground,在应用回到前台的通知中设为true。[[NSNotificationCenter defaultCenter]addObserverForName:UIApplicationWillEnterForegroundNotification object:app queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) {
NSLog(@"Enter Foreground");
self->_isBackToForeground = YES;
}];
然后在observeValueForKeyPath回调中判断下,如果是从后台进入前台时触发的话就不做处理,防止播放器自动停止。
截屏2022-09-06 11.45.04.png