【问题标题】:Connect AVAudioInputNode to AVAudioUnitEffect using AVAudioEngine使用 AVAudioEngine 将 AVAudioInputNode 连接到 AVAudioUnitEffect
【发布时间】:2015-02-04 21:07:18
【问题描述】:

我想使用音频单元效果 (AVAudioUnitEffect) 处理来自设备内置麦克风 (AVAudioInputNode) 的音频。对于我的示例,我使用的是AVAudioUnitReverb。连接 AVAudioUnitReverb 会导致应用程序崩溃。

import UIKit
import AVFoundation

class ViewController: UIViewController {

    let audioEngine = AVAudioEngine()
    let unitReverb = AVAudioUnitReverb()

    var inputNode: AVAudioInputNode!

    override func viewDidLoad() {
        super.viewDidLoad()

        inputNode = audioEngine.inputNode
        audioEngine.attachNode(unitReverb)

        let inputFormat = inputNode.inputFormatForBus(0)
        audioEngine.connect(inputNode, to: unitReverb, format: inputFormat)

        // This line is crashing the application!
        // With this error "AVAudioNode.mm:521: AUSetFormat: error -10868"
        audioEngine.connect(unitReverb, to: audioEngine.outputNode, format: inputFormat)

        audioEngine.startAndReturnError(nil)
    }

}

如果我绕过混响并将inputNode 直接连接到audioEngine.outputNode 没有问题,但是我没有混响:

audioEngine.connect(inputNode, to: audioEngine.outputNode, format: inputFormat)

我做错了什么?

更新

我无意中发现,上面的代码只有在我的Apple EarPods with Remote and Mic 连接时才会导致应用程序崩溃。使用设备的内置麦克风时,我没有问题。那么,为什么我耳机上的麦克风会使应用程序崩溃,但只有在使用AVAudioUnitEffect 时才会崩溃?

【问题讨论】:

    标签: ios avaudioengine


    【解决方案1】:

    经过反复试验,更改格式参数似乎已经解决了这个问题。修改后的代码如下:

    let format = unitReverb.inputFormatForBus(0)
    audioEngine.connect(inputNode, to: unitReverb, format: format)
    audioEngine.connect(unitReverb, to: audioEngine.outputNode, format: format)
    

    【讨论】:

    • 你救了我的命..! :)
    猜你喜欢
    • 1970-01-01
    • 2015-02-03
    • 2015-03-20
    • 2017-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-14
    • 1970-01-01
    相关资源
    最近更新 更多