最近在cocos应用上发现一个比较奇怪的bug,当应用正在调用录音的时候,按下home键回到后台,然后打开一个音乐播放器播放一个音乐,再回到游戏中游戏就会卡死。
之前录音和播放设置是下方:
播放:
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionMixWithOthers error:nil];
录音:
[session setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker error:nil];
恢复正常
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:nil];
个人思路:
音频输出作为硬件资源,对于iOS系统来说是唯一的。
音乐播放器在播放器占用了游戏的硬件资源。导致游戏卡死。
cocos爆出的errorLog也印证了这一点
Log:
D/AudioEngine-inl.mm (198): Audio session is still interrupted, pause director!
经过查询,特别感谢https://www.jianshu.com/p/3e0a399380df
是否引起不支持混音的APP中断。
后面把设置修改为
播放:
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker|AVAudioSessionCategoryOptionMixWithOthers error:nil];
录音:
[session setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionMixWithOthers|AVAudioSessionCategoryOptionDefaultToSpeaker error:nil];
恢复正常:
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategorySoloAmbient withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker|AVAudioSessionCategoryOptionMixWithOthers error:nil];
问题解决。
AVAudioSessionCategoryOptionDefaultToSpeaker 是为了设置扬声器播放,原因:切换成录音状态时会改为听筒播放
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。