iOS音频Error

Posted wycstudy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS音频Error相关的知识,希望对你有一定的参考价值。

最近在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  是为了设置扬声器播放,原因:切换成录音状态时会改为听筒播放

 

以上是关于iOS音频Error的主要内容,如果未能解决你的问题,请参考以下文章

iOS音频Error

关于在各浏览器中插入音频文件的html代码片段

同时播放两个 AVPlayer 音频文件时出现音频故障

通过 BPM 将音轨分割成片段,并使用 Superpowered iOS 分析每个片段

iOS 播放音频文件

iOS设置蓝牙外设播放音频