AVAudioEngine异常-必需的条件为falseformat.sampleRate==hwFormat.sampleRate
创始人
2024-11-12 21:00:41
0

这个异常通常是由于音频引擎与硬件不兼容的采样率所导致的。解决方法是在启动 AVAudioEngine 之前设置正确的采样率,以确保音频引擎和硬件的采样率一致。以下是一个示例代码:

let engine = AVAudioEngine()
let mixer = engine.mainMixerNode
let input = engine.inputNode

// 设置采样率
let desiredSampleRate: Double = 44100.0
let inputFormat = input.inputFormat(forBus: 0)
let outputFormat = AVAudioFormat(commonFormat: .pcmFormatFloat32, sampleRate: desiredSampleRate, channels: 1, interleaved: false)
if let inputFormat = inputFormat, let outputFormat = outputFormat, let converter = AVAudioConverter(from: inputFormat, to: outputFormat) {
    input.installTap(onBus: 0, bufferSize: 4096, format: inputFormat) { (buffer: AVAudioPCMBuffer, time: AVAudioTime) in
        let convertedBuffer = AVAudioPCMBuffer(pcmFormat: outputFormat, frameCapacity: AVAudioFrameCount(outputFormat.sampleRate * buffer.frameLength / inputFormat.sampleRate))!
        var error: NSError?
        let inputBlock: AVAudioConverterInputBlock = { (inNumPackets, outStatus) -> AVAudioBuffer? in
            outStatus.pointee = AVAudioConverterInputStatus.haveData
            return buffer
        }
        converter.convert(to: convertedBuffer, error: &error, withInputFrom: inputBlock)
        mixer.ensureFormat(outputFormat)
        mixer.installTap(onBus: 0, bufferSize: 4096, format: outputFormat) { (buffer: AVAudioPCMBuffer, time: AVAudioTime) in
            // 处理音频数据
        }
    }
}

try engine.start()

相关内容

热门资讯

we辅助poker德之星!德州... we辅助poker德之星!德州微扑克辅助(透视)总是真的有挂(详细辅助wepoke教程)1、金币登录...
德州之星辅助!wepoke有辅... 德州之星辅助!wepoke有辅助挂吗(透视)真是真的有挂(详细辅助科技教程)德州之星辅助辅助器中分为...
德州ai辅助软件!智星德州辅助... 德州ai辅助软件!智星德州辅助(透视)一直是真的有挂(详细辅助2025教程)是一款可以让一直输的玩家...
wepoke真的有挂!哈糖大菠... wepoke真的有挂!哈糖大菠萝怎么弄的(透视)确实真的有挂(详细辅助曝光教程)1、超多福利:超高返...
微扑克ai辅助器苹果版!德扑之... 微扑克ai辅助器苹果版!德扑之星用刷数据吗(透视)从前真的有挂(详细辅助揭秘攻略)是一款可以让一直输...
we辅助poker德之星!德扑... we辅助poker德之星!德扑之星实战(透视)固有真的是有挂(详细辅助AI教程)1、许多玩家不知道w...
wpk发牌逻辑!wpk微扑克真... wpk发牌逻辑!wpk微扑克真的有助辅吗(透视)竟然是有挂(详细辅助wepoke教程)1、wpk发牌...
wpk透视辅助工具!智星德州菠... wpk透视辅助工具!智星德州菠萝有人机吗(透视)先前真的有挂(详细辅助透视教程)1、wpk透视辅助工...
wepoke有辅助挂!gove... wepoke有辅助挂!governorofpoker3有挂吗(透视)原本真的是有挂(详细辅助爆料教程...
aapoker外挂!德扑之星软... aapoker外挂!德扑之星软件介绍(透视)确实是有挂(详细辅助可靠教程)1、aapoker外挂ai...