2

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.

Clemens Tolboom
  • 1,872
  • 18
  • 30
Craig Grummitt
  • 2,945
  • 1
  • 22
  • 34
  • You might get more help if you posted a small project to GitHub that exemplifies the problem. – Aurelius Prochazka Apr 05 '20 at 19:22
  • It really is as simple as creating a new project, adding AudioKit, adding the "Privacy - Microphone Usage Description" to the info.plist and then adding the above TempViewController.swift and connecting it to the main view controller. But if you prefer a GitHub project, here it is: https://github.com/craiggrummitt/AudioKitBug You will need to call 'pod update' to add AudioKit. – Craig Grummitt Apr 06 '20 at 12:36
  • What do you mean that the microphone has "gone silent"? How do you detect that? – Aurelius Prochazka Apr 06 '20 at 21:18
  • I ran it on my phone and I plotted the output of the micBooster and it never "goes silent". What hardware are you testing this on? Are you really saying that the node recorder is not saving the microphone data properly. The example code should show this problem. – Aurelius Prochazka Apr 06 '20 at 21:42
  • If you use headphones, you should hear what is being input through the microphone looping back through the headphones. At a point in time, sometimes it takes 1 minute, sometimes 5-10 mins, what is being recorded stops looping back through the headphones. (And stops being plotted.) I have tested this on an iPhone XR with iOS 13.3, and an iPad Pro (11-inch) with iOS 13.3.1. – Craig Grummitt Apr 07 '20 at 08:53
  • There was no plot in the code you provided. I have been running all day on the iPhone X running 11.4 on the code you provided with a plot added. I am not doubting its happening for you, but I am doubting the example project being what you're testing with. – Aurelius Prochazka Apr 07 '20 at 10:46
  • What kind of headphones are you using? Wondering if they are bluetooth. – Aurelius Prochazka Apr 07 '20 at 11:57
  • Hi Aurelius, thanks for looking into this. You're right that the 'AudioKitBug' project wasn't the original project I was having issues with, but it was the result of paring back my code to the bare bones where I could still see the problem. I added an AKNodeOutputPlot to this project on GitHub and updated my iPhone to 11.4 to match your env. I also ran 'pod update' on both projects. To answer your question, no, I'm not testing with bluetooth headphones. Curiously, after running 'pod update' I am currently finding it difficult to replicate the problem, will test more and let you know how I go. – Craig Grummitt Apr 07 '20 at 16:39

0 Answers0