问题
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))]
Current (simplified) code:
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)")
}
The audioDeviceId (41) is correct, because using a random number gives different errors.
What am I doing wrong?
回答1:
I can't test your code properly due to hardware issues. Which line is throwing the error?
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.
From the docs for mainMixerNode
:
When the property is first accessed the audio engine constructs a singleton main mixer and connects it to the outputNode on demand.
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.
来源:https://stackoverflow.com/questions/52818705/swift-avaudioengine-changing-the-audio-input-device-for-macos