Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop AKMicrophone without stopping AudioKit

Tags:

ios

audiokit

I have an app in which I only want to be recording from one particular screen. On every other screen I would like to not be recording mostly so that audio can still play while the app is in the background without the red recording bar being there. The only way I've been able to do this is to stop AudioKit (AudioKit.stop()) but I'm pretty sure the starting and stopping of AudioKit is causing some very random hard to track down crashes. I have also tried calling stop on the Microphone, and setting input enabled to false but this has not worked either.

I know there is a similar question AudioKit: Can I disable an AKMicrophone without calling AudioKit.stop()?

but the answer doesn't address this.

Is there anyway to stop receiving input from the microphone without stopping the engine?

like image 916
Nathan Avatar asked Dec 13 '17 22:12

Nathan


2 Answers

It depends a bit on your goal with disconnecting the microphone. If you just want to stop processing input and change the AVAudioSession category so that the red bar goes away you could set AKSettings.audioInputEnabled = false but then you'll need to get AVAudioSession's category updated. Currently as far as I can tell there isn't a good way to do this without stopping/starting AudioKit although changing the category without restarting the engine should be possible. One hack that seems to work on first blush is to assign AudioKit.output = AudioKit.output to trigger the didSet side-effect that will update the category via updateSessionCategoryAndOptions().

You could always bypass AudioKit and try to set the shared AVAudioSession's category directly as well. I'm looking into creating a PR for AudioKit to expose updateSessionCategoryAndOptions() but in the meantime you could always make this function public in your own branch and run build_frameworks.sh. Anyway, not great solutions but this is a valid use-case AudioKit should support better in the future!

like image 95
Warpling Avatar answered Sep 18 '22 20:09

Warpling


As a general rule for using Audio Kit you should be employing a proper gain staging pattern. I don't know if you have any experiences with any DAW's (protools, logic, cubase etc) but you should view AudioKit as a DAW... in code.

AudioKit.start()

is your master output and I honestly see no reason to ever stop it during your application. Instead you should mute the master output of audiokit to 'kill the sound'

As for your question, AKMicrophone does have both a start and a stop property, you should just be able to call stop!

let mic = AKMicrophone

mic.start(AKMicrophone)

mic.stop(AKMicrophone)
like image 45
Axemasta Avatar answered Sep 19 '22 20:09

Axemasta