AVAudioEngine录音崩溃, reason: ‘format.sampleRate == hwFormat.sampleRate

AVAudioEngine录音崩溃, reason: ‘format.sampleRate == hwFormat.sampleRateAVAudioEngine录音频时偶发崩溃报错信息大致如下:2021-12-1520:12:38.429028+0800*[1659:708511]NSURLConnectionfinishedwitherror-code-1002″AudioRecorder创建Audio缓存文件夹成功/var/mobile/Containers/Data/Application//Library/Caches/Audio”2021-12-1520:13:30.762736+0800***

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

AVAudioEngine录音频时偶发崩溃
报错信息大致如下:
2021-12-15 20:12:38.429028+0800 *[1659:708511] NSURLConnection finished with error – code -1002
“AudioRecorder 创建Audio缓存文件夹成功 /var/mobile/Containers/Data/Application/
/Library/Caches/Audio”
2021-12-15 20:13:30.762736+0800 ***[1659:708411] [avae] AVAEInternal.h:76 required condition is false: [AVAudioIONodeImpl.mm:1158:SetOutputFormat: (format.sampleRate == hwFormat.sampleRate)]
2021-12-15 20:13:30.764037+0800 ***[1659:708411] *** Terminating app due to uncaught exception ‘com.apple.coreaudio.avfaudio’, reason: ‘required condition is false: format.sampleRate == hwFormat.sampleRate’
*** First throw call stack:
(0x1989799d8 0x1accffb54 0x19888850c 0x1a88e2984 0x1a898c998 0x1a8923038 0x1a892945c 0x1a89a4f4c 0x1a899f414 0x10626bad4 0x10626b130 0x10627f8bc 0x10480ecc0 0x10480e8f4 0x10621e100 0x10621b720 0x10621dcf8 0x10480e8a0 0x10480ed20 0x19b1eaf38 0x19ab80184 0x19ab804c8 0x19ab7ee00 0x19ad65350 0x19ad634e0 0x19b2274cc 0x19b202b0c 0x19b285078 0x19b289818 0x19b280afc 0x1988f9bf0 0x1988f9af0 0x1988f8e38 0x1988f33e0 0x1988f2ba0 0x1af65b598 0x19b1e42f4 0x19b1e9874 0x1ac073b54 0x104a54194 0x104a5410c 0x104a5425c 0x1985d1568)
libc++abi.dylib: terminating with uncaught exception of type NSException
*** Terminating app due to uncaught exception ‘com.apple.coreaudio.avfaudio’, reason: ‘required condition is false: format.sampleRate == hwFormat.sampleRate’
terminating with uncaught exception of type NSException
*** Terminating app due to uncaught exception ‘com.apple.coreaudio.avfaudio’, reason: ‘required condition is false: format.sampleRate == hwFormat.sampleRate’
terminating with uncaught exception of type NSException

排查发现是inputNode和mainMixerNode的format不一致
(lldb) po inputFormat
<AVAudioFormat ***: 1 ch, 48000 Hz, Float32>

(lldb) po outputFormat
<AVAudioFormat 0x282b95a40: 2 ch, 44100 Hz, Float32, non-inter>

原代码:

 let audioEngine = AVAudioEngine()
 let filePlayer = AVAudioPlayerNode()
 let rateEffect = AVAudioUnitTimePitch()

 audioEngine.attach(filePlayer)
 audioEngine.attach(rateEffect)

 let outputFormat = audioEngine.mainMixerNode.outputFormat(forBus: 0)
        
 //崩溃在这行代码
 audioEngine.connect(audioEngine.inputNode, to: audioEngine.mainMixerNode, fromBus: 0, toBus: 0, format: outputFormat)
        
        audioEngine.mainMixerNode.installTap(onBus: 0, bufferSize: 4096, format: outputFormat) { 
    [weak self] pcmBuffer, when in
        ...
        }

解决办法:将format都改成48000
参考:https://gist.github.com/tad-iizuka/1ca07ca2045a8c6d11e22d15812f7e15
在这里插入图片描述

修改后代码

        let audioEngine = AVAudioEngine()
        let filePlayer = AVAudioPlayerNode()
        let rateEffect = AVAudioUnitTimePitch()

        audioEngine.attach(filePlayer)
        audioEngine.attach(rateEffect)
        
        let inputFormat = audioEngine.inputNode.inputFormat(forBus: 0)
        let outputFormat = audioEngine.mainMixerNode.outputFormat(forBus: 0)
        
        //修改format为inputNode的format,防止录音崩溃
        audioEngine.connect(audioEngine.inputNode, to: audioEngine.mainMixerNode, fromBus: 0, toBus: 0, format: inputFormat)
        
        audioEngine.mainMixerNode.installTap(onBus: 0, bufferSize: 4096, format: outputFormat) { 
    [weak self] pcmBuffer, when in
        ...
        }
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/181074.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • http请求过程图解_性能优化的方法

    http请求过程图解_性能优化的方法之前有整理过一部分知识点,一直没有发布,因为都是有关CSS方面的零散内容;现在想想无论做什么都需要慢慢积累,所以还是决定将之前整理的相关内容验证之后慢慢分享给你们,现在看到感觉还挺有意思。好了废话不多说,直接上代码以及图例(为了让大家方便阅读,都有自己验证过程的一些图片作为分享)。1.什么是Web前端1.Web前端本质上是一种GUI软件,可以直接借鉴其它GUI软件系统架构设计方法。1.什么是GUI软件?1.图形化用户界面:

    2022年9月1日
    8
  • javascript:是什么意思

    javascript:是什么意思JavaScript是一种基于对象和事件驱动并具有相对安全性的客户端脚本语言。同时也是一种广泛用于客户端Web开发的脚本语言,常用来给HTML网页添加动态功能,比如响应用户的各种操作。它最初由网景公司

    2022年8月5日
    7
  • 502 Bad Gateway 常见解决思路

    502 Bad Gateway 常见解决思路一般在访问某些网站或者我们在做本地测试的时候,服务器突然返回502BadGatewayNginx,这种问题相信大家也遇到不少了,这里我再总结下几种处理方式,有缺少或者错误的希望有大神能指出。一般的思维:502,说明服务器没有响应,也就是我们的web服务器没有接到有效的信息导致的。产生错误的原因主要是:连接超时,我们向服务器发送请求由于服务器当前链接太多,导致服务器方面无…

    2022年6月29日
    34
  • python数组_js数组添加数据

    python数组_js数组添加数据记linuxshell的两个小技巧:shell数组和字符串判断最近在使用shell写脚本的时候,想实现python中两个很简单的功能:1:判断一个字符串是否包含另一个字符串。2:怎么用实现python的列表功能。这里跟大家分享一下。1:判断一个字符串是否包含另一个字符串:123456string=”abcdefg”…文章余二五2017-11-151010浏览量Python数据类型Python…

    2022年8月13日
    4
  • linux 修改mysql端口「建议收藏」

    linux 修改mysql端口「建议收藏」1)先关闭MySQL的进程2)更改端口vim/etc/my.cnf添加:port=33083)重启MySQL服务:servicemysqldrestart4)查看MySQL的端口号3.1.登录mysqlmysql-uroot-p3.2.查看当前端口号showglobalvariableslike‘port’;…

    2022年10月3日
    2
  • 阿里巴巴代码规范插件 idea(阿里数据库开发规范最新)

    https://github.com/alibaba/p3c1、下载离线安装插件https://plugins.jetbrains.com/plugin/10046-alibaba-java-coding-guidelines2.Settings>>Plugins>>Installpluginfromdisk…,selectthedow…

    2022年4月10日
    59

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号