iOS音频Error

博主在cocos应用中发现,调用录音时按home键回后台,打开音乐播放器再回到游戏会卡死。分析认为,iOS系统音频输出硬件资源唯一,音乐播放器占用资源导致游戏卡死,cocos的errorLog也印证了这点。修改设置后问题解决。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

 

转载于:https://www.cnblogs.com/wycstudy/p/10954642.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值