我有采样率8khz的错误来读取实时音频缓冲区

时间:2016-11-27 18:25:57

标签: swift3 avfoundation xcode8

使用以下代码我有错误:

var engine = AVAudioEngine()
    let input = engine.inputNode!
    let bus = 0

    let mixer = AVAudioMixerNode()
    engine.attach(mixer)

    engine.connect(input, to: mixer, format: input.outputFormat(forBus: 0))
    //pcmFormatFloat64 -- pcmFormatFloat32
    let fmt = AVAudioFormat(commonFormat: .pcmFormatFloat32, sampleRate: 8000, channels: 1, interleaved: true)

    mixer.installTap(onBus: bus, bufferSize: 512, format: fmt) { (buffer, time) -> Void in
        // 8kHz buffers!
        print(buffer.format)
    }

    try! engine.start()
}

错误:kAudioUnitErr_TooManyFramesToProcess : inFramesToProcess=1024, mMaxFramesPerSlice=768

采样率441khz一切正常,但8khz不是

这段代码有什么问题?

1 个答案:

答案 0 :(得分:0)

我在8kHz遇到了同样的错误信息,并通过设置我的音频会话实例的首选采样率和捕获持续时间来修复它:

let audioSession = AVAudioSession.sharedInstance()
try audioSession.setPreferredSampleRate(sampleRate)
try audioSession.setPreferredIOBufferDuration(TimeInterval(processingInterval))