我想处理来自设备内置麦克风的音频(AVAudioInputNode
)带有音频单元效果(AVAudioUnitEffect
)。对于我的例子,我正在使用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)
我究竟做错了什么?
Update
我无意中发现上面的代码只会在我的带线控和麦克风的 Apple EarPods http://store.apple.com/us/product/MD827LL/A/apple-earpods-with-remote-and-mic连接。使用设备的内置麦克风时,我没有任何问题。那么,为什么我的耳机上的麦克风会使应用程序崩溃,但仅在使用耳机时才发生?AVAudioUnitEffect
?
经过一番尝试和错误后,更改格式参数似乎解决了这个问题。修改后的代码如下所示:
let format = unitReverb.inputFormatForBus(0)
audioEngine.connect(inputNode, to: unitReverb, format: format)
audioEngine.connect(unitReverb, to: audioEngine.outputNode, format: format)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)