From e83eeb7466be5d769327d1054b1fb0dba05bb653 Mon Sep 17 00:00:00 2001 From: Vadym Markov Date: Sun, 4 Sep 2016 17:53:11 +0200 Subject: [PATCH 1/2] Make audio engine optional --- .../Units/InputSignalTracker.swift | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/Source/SignalTracking/Units/InputSignalTracker.swift b/Source/SignalTracking/Units/InputSignalTracker.swift index ad52dda..2244821 100644 --- a/Source/SignalTracking/Units/InputSignalTracker.swift +++ b/Source/SignalTracking/Units/InputSignalTracker.swift @@ -12,7 +12,7 @@ public class InputSignalTracker: SignalTracker { var audioChannel: AVCaptureAudioChannel? let captureSession = AVCaptureSession() - private var audioEngine: AVAudioEngine! + private var audioEngine: AVAudioEngine? private let session = AVAudioSession.sharedInstance() private let bus = 0 @@ -45,7 +45,7 @@ public class InputSignalTracker: SignalTracker { audioEngine = AVAudioEngine() - guard let inputNode = audioEngine.inputNode else { + guard let inputNode = audioEngine?.inputNode else { throw Error.InputNodeMissing } @@ -63,17 +63,15 @@ public class InputSignalTracker: SignalTracker { } captureSession.startRunning() - audioEngine.prepare() - try audioEngine.start() + audioEngine?.prepare() + try audioEngine?.start() } public func stop() { - if audioEngine != nil { - audioEngine.stop() - audioEngine.reset() - audioEngine = nil - captureSession.stopRunning() - } + audioEngine?.stop() + audioEngine?.reset() + audioEngine = nil + captureSession.stopRunning() } func setupAudio() { From 04fc14544a703a46a4a30f84b110a2742328734d Mon Sep 17 00:00:00 2001 From: Vadym Markov Date: Sun, 4 Sep 2016 17:54:14 +0200 Subject: [PATCH 2/2] Add guard --- Source/SignalTracking/Units/InputSignalTracker.swift | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Source/SignalTracking/Units/InputSignalTracker.swift b/Source/SignalTracking/Units/InputSignalTracker.swift index 2244821..240e0cf 100644 --- a/Source/SignalTracking/Units/InputSignalTracker.swift +++ b/Source/SignalTracking/Units/InputSignalTracker.swift @@ -68,6 +68,10 @@ public class InputSignalTracker: SignalTracker { } public func stop() { + guard audioEngine != nil else { + return + } + audioEngine?.stop() audioEngine?.reset() audioEngine = nil