Audiokit: AKMicrophone and Red Bar Forever

Created on 12 Apr 2019  ·  6Comments  ·  Source: AudioKit/AudioKit

Hi, I have some issue with red bar which is appearing with recording.

My steps are;

Record Step:

  • Setting Session .playAndRecord
  • Initializing AKMicrophone
  • AudioKit.Start
    (switch the app background mode _Red Bar is shown as expected, OK._)
    So I continue...

Playback Step:

  • Stop AudioKit
  • Stop connections (mixer, instruments etc.)
    microphone?.stop()
    microphone?.detach()
    microphone = nil
  • Change Session to .playback
  • Reconnect the Nodes (mixer etc)
  • AudioKit.Start
    (switch the app background mode _Red Bar is still there!_)

After my research in framework I noticed that;
In Record Step; when we set the mic. device
...
microphone?.setDevice(frontMicrophone)
...
It is setting AVAudioSession.sharedInstance().setPreferredInput(device) and this connection is persisting even after; AudioKit.disconnectAllInputs() and Stop|Start AudioKit. That's why Red Bar Forever

My expectation:
I think, AudioKit.disconnectAllInputs() should remove this connection.
Or restarting AudioKit should set it to initial/nil value

If non of these suppose to accomplish this, Can we add a new method like; resetPreferredInput() ?

All 6 comments

I think both of your expectations should be put into place and maybe the method too. Do you want to add this and make a PR?

@trevor-sonic I was also experiencing this issue when trying to switch between recording and a simple playback mode. I do agree with your expectation that AudioKit should handle this.

To get around the issue for now, I decided to always breakdown the nodes and reset AudioKit when transitioning between recording and playback. I came up with these steps that seemed to work...stop all the nodes and AudioKit followed by calling shutdown on AudioKit and resetting the AudioKit output to nil. I couldn't find any information whether creating and breaking down nodes causes any sort of memory problems. 🤷‍♂️

do {
   mic?.stop()
   <another_node>?.stop()

   try AudioKit.stop()
   try AudioKit.shutdown()
   AudioKit.output = nil

   mic = nil
} catch {
// TODO: Handle error
}

Hi @mglodack, AudioKit.output = nil is changing the Red Bar Forever issue but it is so expensive for me because all the connected nodes, mixer... must be reinitialized ( I actually just use .stop() when I restart AudioKit for session changes.

@aure I'll dig into it but I'm not sure, it looks like it's beyond my knowledge.

Thank you all, at least now I know about the issue more precisely.

I've tried many work around creating a function for testing to reset/forget the input already set.
Something like;
func resetInputDevice(){ do{ try AVAudioSession.sharedInstance().setPreferredInput(nil) }catch{ print(error) } }

But it is not unset the mic.
Apple says (in the var definition) "Setting a nil value will clear the preference." but it doesn't work in the way that I expect.
Anyway only way to remove this Red Bar Forever is @mglodack 's method.
I just wanted to let the others know, if they struggle with the same problem.

Did this issue happen to get addressed in the 5.0 beta for future release?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

gm3197 picture gm3197  ·  10Comments

impmaarten picture impmaarten  ·  5Comments

crashingbooth picture crashingbooth  ·  4Comments

vojtabohm picture vojtabohm  ·  8Comments

trevor-sonic picture trevor-sonic  ·  5Comments