AudioKit: AKNodeRecorder randomly stops microphone

264 Views Asked by At

I've got a strange one - I'm using an AKRecorder to record short segments from the AKMicrophone. However randomly every now and again, the microphone goes silent. Sometimes it happens fairly quickly, sometimes it takes a few minutes.

I've stripped back the code to the minimum to replicate this problem. Basically, I'm doing the following:

  1. Passing an AKMicrophone instance into an AKBooster instance.
  2. Using the AKBooster instance to instantiate both an AKNodeRecorder and an AKMixer.
  3. Passing the AKMixer into AudioKit output.
  4. Starting the AKRecorder recording.
  5. Firing a timer every 2 seconds that resets the AKNodeRecorder and requesting it to start recording again.

Here is the code:

    import UIKit
    import AudioKit
    import AudioKitUI

    class TempViewController: UIViewController {
        var recorder: AKNodeRecorder!
        var micBooster: AKBooster!
        var mainMixer: AKMixer!
        var timer: Timer?

        let mic = AKMicrophone()

        required init?(coder aDecoder: NSCoder) {
            super.init(coder: aDecoder)


            do {
                try AKSettings.setSession(category: .playAndRecord, with: .allowBluetoothA2DP)
            } catch {
                AKLog("Could not set session category.")
            }

            AKSettings.defaultToSpeaker = true
            micBooster = AKBooster(mic)

            do {
                recorder = try AKNodeRecorder(node: micBooster)
            } catch {
                AKLog("Couldn't create Recorder")
            }

            mainMixer = AKMixer(micBooster)

            AudioKit.output = mainMixer
            do {
                try AudioKit.start()
            } catch {
                AKLog("AudioKit did not start!")
            }
        }

        override func viewDidLoad() {
            super.viewDidLoad()
            self.timer = Timer.scheduledTimer(timeInterval: 2, target: self, selector: #selector(repeatRecorder), userInfo: nil, repeats: true)
            do {
                try recorder.record()
            } catch { AKLog("Error on first record")}
        }

        @objc func repeatRecorder() {
            do {
                try self.recorder.reset()
                try self.recorder.record()
            } catch { AKLog("Errored recording.") }
        }
    }

I would much appreciate the help if anyone has any idea either:

  1. how to prevent the microphone from going silent.
  2. how to detect when the mic has gone silent and wake it up again.

If you would like a github project that illustrates the problem, here it is: https://github.com/craiggrummitt/AudioKitBug

You will need to call 'pod update' to add AudioKit.

0

There are 0 best solutions below