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)
上一篇 2022年10月17日 上午10:00
下一篇 2022年10月17日 上午10:16


相关推荐

  • Claude code学习记录

    Claude code学习记录

    2026年3月14日
    3
  • Hook技术【移动端&&PC端详解】「建议收藏」

    Hook技术【移动端&&PC端详解】「建议收藏」最近面试说到了这个hook技术,其实就是钩子函数,但是具体如何应用需要一探究竟,私下总结一下。文章目录移动端的hook技术应用1.whatisHook技术(移动端)2.Hook技术实现的步骤3.在移动开发中的应用:3.1使用hook技术实现免注册式跳转Windows端应用1.whatishook(钩子)2.Hook分类3.Hook工作原理Hook简介微软的MSDN中,…

    2022年5月26日
    55
  • nginx 400 报错,故障排查

    nginx 400 报错,故障排查nginx的400错误比较难查找原因,因为此错误并不是每次都会出现的,另外,出现错误的时候,通常在浏览器和日志里看不到任何有关提示。经长时间观察和大量试验查明,此乃requestheader过大所引起,request过大,通常是由于cookie中写入了较大的值所引起。解决办法这:在nginx.conf中,将client_header_buffer_size和large_cl…

    2022年5月1日
    74
  • C++之constexpr详解

    C++之constexpr详解constexpr表达式是指值不会改变并且在编译过程就能得到计算结果的表达式。声明为constexpr的变量一定是一个const变量,而且必须用常量表达式初始化:constexprintmf=20;//20是常量表达式constexprintlimit=mf+1;//mf+1是常量表达式constexprintsz=size();//之后当siz…

    2025年8月6日
    4
  • SAP Web IDE本地环境搭建「建议收藏」

    SAP Web IDE本地环境搭建「建议收藏」SAPWebIDE本地环境搭建1、JDK的安装及配置环境变量可通过官网自行下载JDK:JavaDownloads|Oracle直接傻瓜式安装至默认路径“C:\ProgramFiles\Java\jdk-17.0.2”,然后配置环境变量“JAVA_HOME”、“CLASSPATH”和“PATH”。JAVA_HOME:C:\ProgramFiles\Java\jdk-17.0.2CLASSPATH:.;%JAVA_HOME%\lib;%JAVA_HOME%\lib\

    2022年10月18日
    10
  • HTTP请求的完全过程

    HTTP请求的完全过程HTTP 请求的完全过程 1 1 浏览器根据域名解析 IP 地址浏览器根据访问的域名找到其 IP 地址 DNS 查找过程如下 浏览器缓存 首先搜索浏览器自身的 DNS 缓存 缓存的时间比较短 大概只有 1 分钟 且只能容纳 1000 条缓存 看自身的缓存中是否是有域名对应的条目 而且没有过期 如果有且没有过期则解析到此结束 系统缓存 如果浏览器自身的缓存里面没有找到对应的条目 那么浏览器会

    2026年3月20日
    2

发表回复

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

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