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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Codeforces 486C Palindrome Transformation(贪心)[通俗易懂]

    Codeforces 486C Palindrome Transformation(贪心)

    2022年1月31日
    47
  • OJ错误提示类型

    OJ错误提示类型答案正确(Acepted,AC)  恭喜你!所提交的代码通过了数据!这个评测结果应该是大家最喜欢见到的,也非常好理解。如果是单点测试,那么没通过一组数据,就会返回一个Accepted;如果是多点测试,那么只有当通过了所有数据时,才会返回Accepted。编译错误(CompileError,CE)  很显然,如果代码没有办法通过编译,那么就会返回CompileError。这是要…

    2022年6月24日
    18
  • JAVA将string转化为int(int怎么转string)

    1如何将字串String转换成整数int?A.有两个方法:1).inti=Integer.parseInt([String]);或i=Integer.parseInt([String],[intradix]);2).inti=Integer.valueOf(my_str).intValue();注:字串转成Double,Float,Lo

    2022年4月12日
    34
  • python中dtype、type()、astype()区别

    python中dtype、type()、astype()区别(1)type()是python内置的函数。type()返回数据结构类型(list、dict、numpy.ndarray等)(2)dtype返回数据元素的数据类型(int、float等)(3)astype()改变np.array中所有数据元素的数据类型。————————————备注:1)由于list、dict等可以包含不同的数据类型,因此没有dtype属性2)np.array中要求所有元素属于同一数据类型,因此有dtype属性备注:能用dtype()才能用astype().

    2022年5月2日
    69
  • 使用C++解决八数码问题

    使用C++解决八数码问题八数码问题问题描述:通过单步移动把下面的矩阵移动成1-8环绕一周的矩阵(即0在中间,1-8顺序排成一圈,1在哪无所谓)217860345283164705 \begin{matrix} 2&8&3\\ 1&6&4\\ 7&0&5\\ \end{matrix}(1)分别用宽度和深度搜索进行;(2)假设启发式的方程为f(n)=d(n)+…

    2022年7月12日
    23
  • html语言添加下划线,HTML页面中怎么文本添加下划线?[通俗易懂]

    html语言添加下划线,HTML页面中怎么文本添加下划线?[通俗易懂]怎么在HTML页面中给文本添加下划线?下面本篇文章就来给大家介绍一下HTML、CSS给文本添加下划线的方法,希望对大家有所帮助。HTML添加下划线在HTML中可以使用标签定义下划线文本,即为文本添加下划线。下划线标签告诉浏览器把加入到u标签的文本加下划线样式呈现显示给浏览者。对于所有浏览器来说,这意味着要把这段文字加下划线样式方式呈现给大家显示。语法:我被加下划线了说明:标签定义与常规文本风格不…

    2022年6月3日
    38

发表回复

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

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