2

I am studying the audio unit development. First I want to build a macOS app that simply fetch the audio from microphone then send it out to speaker that we can hear it.

engine = AVAudioEngine()
    
let hardwareFormat = engine.outputNode.outputFormat(forBus: 0)
engine.connect(engine.mainMixerNode, to: engine.outputNode, format: hardwareFormat)

do {
    engine.prepare()
    try engine.start()
    print("engine start success")
} catch {
    fatalError("engine start error:\(error)")
}

I create an AVAudioEngine object and connect the main mixer node to the output node. The engine start successfully. But I can't hear any sound. Then I guess maybe the inputNode did not connect to the main mixer node.

let hardwareFormat = engine.outputNode.outputFormat(forBus: 0)
engine.connect(engine.mainMixerNode, to: engine.outputNode, format: hardwareFormat)
engine.connect(engine.inputNode, to: engine.mainMixerNode, format: engine.mainMixerNode.inputFormat(forBus: 0))

It crashes and reports: required condition is false: IsFormatSampleRateAndChannelCountValid(hwFormat)

The info.plist also included the Privacy Mic Usage Description

I got the solution which is adding the hardened runtime capability (Audio In)

Hao Wu
  • 111
  • 6
  • Do you ask for permission to use the microphone in info.plist ? https://stackoverflow.com/a/38498347/8876321 Privacy microphone usage description – fdcpp Mar 30 '21 at 17:27
  • @fdcpp I have got the permission to use the microphone still can not work – Hao Wu Mar 31 '21 at 01:20
  • Is Mic access checked in hardened runtime? – fdcpp Mar 31 '21 at 06:53
  • @fdcpp First I check the mic permission status, when authorized, then init the audioengine stuff – Hao Wu Mar 31 '21 at 08:01
  • 1
    @fdcpp Thank You! It works after adding the hardened runtime capabilities! – Hao Wu Mar 31 '21 at 08:46
  • I can't easily find another question with the same error so I would answer this by exaplining what you did and adding in a screen grab of the hardened run time _with_ **Audio Input** checked in hardened runtime signing and capabilities. – fdcpp Mar 31 '21 at 09:32
  • It wouldn't hurt to edit your question and say that info.plist also included the _Privacy Mic Usage Description_ – fdcpp Mar 31 '21 at 09:33

0 Answers0