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:
- Passing an
AKMicrophone
instance into anAKBooster
instance. - Using the
AKBooster
instance to instantiate both anAKNodeRecorder
and anAKMixer
. - Passing the
AKMixer
into AudioKit output. - Starting the
AKRecorder
recording. - 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:
- how to prevent the microphone from going silent.
- 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.