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


相关推荐

  • 华为交换机链路聚合trunk_华为删除链路聚合命令

    华为交换机链路聚合trunk_华为删除链路聚合命令文章目录一、为什么要用到聚合二、链路聚合三、拓扑四、基本配置五、部署LinkAggregation-链路聚合一、为什么要用到聚合1、二层环境为什么要用到聚合(1)接入层的流量剧增;(2)现行存储数据剧增;(3)监控网络运行大数据流量;

    2025年7月27日
    3
  • SQL之CASE WHEN用法详解

    SQL之CASE WHEN用法详解简单CASEWHEN函数:CASESCOREWHEN’A’THEN’优’ELSE’不及格’ENDCASESCOREWHEN’B’THEN’良’ELSE’不及格’ENDCASESCOREWHEN’C’THEN’中’ELSE’不及格’END等同于,使用CASEWHEN条件表达式函数实现:CASEWHENSCORE=’A’……

    2022年6月21日
    28
  • drop in session_drop constraint

    drop in session_drop constraint//aspx’>//aspx.csprotectedvoidPage_Load(objectsender,EventArgse){if(!IsPostBack){SqlConnectioncn=newSqlConnection(@”Server=.\SQLEXPress;uid=sa;pwd=password;Da

    2022年8月31日
    2
  • onpropertychange兼容示例[通俗易懂]

    [code="js"]//当状态改变的时候执行的函数functionhandle(){document.getElementById(‘msg’).innerHTML=’输入的文字长度为:’+document.getElementById(‘txt’).value.length;}//firefox下检测状态改变只能用oninput,且需要用addE…

    2022年4月9日
    53
  • FPGA综合项目——SDRAM控制器

    FPGA综合项目——SDRAM控制器FPGA综合项目——SDRAM控制器目录整体框架串口接收模块接收模块测试仿真串口发送模块发送模块测试仿真整体框架串口接收模块接收模块测试仿真串口发送模块发送模块测试仿真

    2022年7月25日
    10
  • sql去掉重复的行_select去掉重复记录

    sql去掉重复的行_select去掉重复记录有重复数据主要有一下几种情况:1.存在两条完全相同的纪录这是最简单的一种情况,用关键字distinct就可以去掉example:selectdistinct*fromtable(表名)where(条件)2.存在部分字段相同的纪录(有主键id即唯一键)如果是这种情况的话用distinct是过滤不了的,这就要用到主键id的唯一性特点及groupby分组example:select*…

    2022年10月2日
    2

发表回复

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

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