Swift AVAudioEngine:更改MacOS的音频输入设备 [英] Swift AVAudioEngine: Changing the Audio Input Device for MacOS
问题描述
我正在尝试更改用于收听传入音频的输入设备. 我已经尝试了多种解决方案,但是大多数在准备和启动音频引擎时都会遇到以下错误:
I'm trying to change the input device used to listen to incoming audio. I've tried a number of solutions, but most end up with the following error when preparing and starting the audio-engine:
AVAEInternal.h:82:_AVAE_CheckAndReturnErr: required condition is false: [AVAudioEngineGraph.mm:1295:Initialize: (IsFormatSampleRateAndChannelCountValid(outputHWFormat))]
当前(简体)代码:
var engine = AVAudioEngine()
var inputDeviceID: AudioDeviceID = 41 // another audio input device
let sizeOfAudioDevId = UInt32(MemoryLayout<AudioDeviceID>.size)
let error = AudioUnitSetProperty(engine.inputNode.audioUnit!, kAudioOutputUnitProperty_CurrentDevice, kAudioUnitScope_Global, 0, &inputDeviceID, sizeOfAudioDevId)
if error > 0
{
print(error)
}
let inputNode = engine.inputNode
engine.connect(inputNode, to: engine.mainMixerNode, format: nil)
engine.connect(engine.mainMixerNode, to: engine.outputNode, format: nil)
engine.prepare()
do
{
try engine.start()
}
catch
{
print("Failed to start the audio input engine: \(error)")
}
audioDeviceId(41)是正确的,因为使用随机数会产生不同的错误.
The audioDeviceId (41) is correct, because using a random number gives different errors.
我在做什么错了?
推荐答案
由于硬件问题,我无法正确测试您的代码.哪一行引发错误?
I can't test your code properly due to hardware issues. Which line is throwing the error?
您到底想达到什么目的?自从我看过AVAudioEngine
已经有一段时间了,但是我认为没有必要连接内置引擎节点.至少没有输出.
What exactly are you trying to achieve? It's been a while since I've looked at AVAudioEngine
, but I don't think it's necessary to connect the built-in engine nodes. At least not the output.
mainMixerNode
的文档:
第一次访问该属性时,音频引擎会构建一个单例主混音器,并根据需要将其连接到outputNode.
When the property is first accessed the audio engine constructs a singleton main mixer and connects it to the outputNode on demand.
我还将尝试使用node.outputFormat(forBus: 0)
检查有问题的节点的格式,并可能像在
I'd also try checking the format of the offending nodes with node.outputFormat(forBus: 0)
, and maybe explicitly setting the format when making connections like in this answer.
这篇关于Swift AVAudioEngine:更改MacOS的音频输入设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!