iOS音频Error

最近在cocos应用上发现一个比较奇怪的bug,当应用正在调用录音的时候,按下home键回到后台,然后打开一个音乐播放器播放一个音乐,再回到游戏中游戏就会卡死。

之前录音和播放设置是下方:

播放:

1
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionMixWithOthers error:nil];

 录音:

1
[session setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker error:nil];

 恢复正常

1
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:nil];

 

个人思路:

音频输出作为硬件资源,对于iOS系统来说是唯一的。

音乐播放器在播放器占用了游戏的硬件资源。导致游戏卡死。

cocos爆出的errorLog也印证了这一点

Log:

1
D/AudioEngine-inl.mm (198): Audio session is still interrupted, pause director!

 

经过查询,特别感谢https://www.jianshu.com/p/3e0a399380df

 是否引起不支持混音的APP中断。

后面把设置修改为

播放:

1
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker|AVAudioSessionCategoryOptionMixWithOthers error:nil];

 录音:

1
[session setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionMixWithOthers|AVAudioSessionCategoryOptionDefaultToSpeaker error:nil];

 恢复正常:

1
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategorySoloAmbient withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker|AVAudioSessionCategoryOptionMixWithOthers error:nil];

 问题解决。

1
AVAudioSessionCategoryOptionDefaultToSpeaker  是为了设置扬声器播放,原因:切换成录音状态时会改为听筒播放

 

!!!

emmm,上面方法虽然可以解决问题,但是会出现一点:每次录音都要切换状态会重新走一遍准备录音环境的流程,在应用中就是会卡顿一丢丢。如果对程序有较高的要求,出现这个问题明显是不行的。

最后解决方案:

在每次调用录音单例类的时候调用一下状态切换

1
2
3
4
+ (void)initRecorder{
    AVAudioSession *session = [AVAudioSession sharedInstance];
    [session setActive:YES error:nil];
    [session setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionMixWithOthers|AVAudioSessionCategoryOptionDefaultToSpeaker error:nil];

然后切换播放环境不改变category,因为前面用的是PlayAndRecord。

什么时候切换成默认状态呢,这个需要cocos端在退出需要录音环境的页面是来通知一下iOS端,然后我们

 

1
2
3
4
5
6
7
8
9
+ (void)finish{
    if (player.playing) {
        [player stop];
    }
    if (recorder.recording){
        [recorder stop];
    }
    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker|AVAudioSessionCategoryOptionMixWithOthers error:nil];
}

 

 这个真的是我认为的最优解了。。有更好的解放方案的同学可以在下方留言一起探讨。

posted @   小师傅啊小师傅  阅读(1189)  评论(0编辑  收藏  举报
编辑推荐:
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
阅读排行:
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
历史上的今天:
2018-05-31 求数组的平均值、最大值、最小值、总和
点击右上角即可分享
微信分享提示