From 73e6eb1cca7cae312378616a5f4dc74e5c57752f Mon Sep 17 00:00:00 2001 From: Adam Vongrej Date: Wed, 24 Apr 2024 18:55:21 +0200 Subject: [PATCH] PR to fix issue with first word missed on iOS on 2nd+ calls to listenForSpeech --- .../ios/Classes/SwiftSpeechToTextPlugin.swift | 28 +++++++++++-------- speech_to_text/pubspec.yaml | 2 +- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/speech_to_text/ios/Classes/SwiftSpeechToTextPlugin.swift b/speech_to_text/ios/Classes/SwiftSpeechToTextPlugin.swift index 8c02118..1d68426 100644 --- a/speech_to_text/ios/Classes/SwiftSpeechToTextPlugin.swift +++ b/speech_to_text/ios/Classes/SwiftSpeechToTextPlugin.swift @@ -84,7 +84,7 @@ public class SwiftSpeechToTextPlugin: NSObject, FlutterPlugin { private var listening = false private var stopping = false private let audioSession = AVAudioSession.sharedInstance() - private let audioEngine = AVAudioEngine() + private var audioEngine: AVAudioEngine? private var inputNode: AVAudioInputNode? private let jsonEncoder = JSONEncoder() private let busForNodeTap = 0 @@ -234,16 +234,20 @@ public class SwiftSpeechToTextPlugin: NSObject, FlutterPlugin { onDeviceStatus = localRecognizer.supportsOnDeviceRecognition } recognizer?.delegate = self - inputNode = audioEngine.inputNode - guard inputNode != nil else { - os_log("Error no input node", log: pluginLog, type: .error) - sendBoolResult( false, result ); - return - } setupListeningSound() sendBoolResult( true, result ); } + + private func initAudioEngine( _ result: @escaping FlutterResult) -> Bool { + audioEngine = AVAudioEngine() + inputNode = audioEngine?.inputNode + if inputNode == nil { + os_log("Error no input node", log: pluginLog, type: .error) + sendBoolResult( false, result ); + } + return inputNode != nil + } private func setupRecognizerForLocale( locale: Locale ) { if ( previousLocale == locale ) { @@ -316,7 +320,7 @@ public class SwiftSpeechToTextPlugin: NSObject, FlutterPlugin { stopAllPlayers() do { try trap { - self.audioEngine.stop() + self.audioEngine?.stop() } } catch { @@ -400,7 +404,9 @@ public class SwiftSpeechToTextPlugin: NSObject, FlutterPlugin { } sound.play() } - self.audioEngine.reset(); + if !initAudioEngine(result) { + return + } if(inputNode?.inputFormat(forBus: 0).channelCount == 0){ throw SpeechToTextError.runtimeError("Not enough available inputs.") } @@ -442,8 +448,8 @@ public class SwiftSpeechToTextPlugin: NSObject, FlutterPlugin { // if ( inErrorTest ){ // throw SpeechToTextError.runtimeError("for testing only") // } - self.audioEngine.prepare() - try self.audioEngine.start() + self.audioEngine?.prepare() + try self.audioEngine?.start() if nil == listeningSound { listening = true self.invokeFlutter( SwiftSpeechToTextCallbackMethods.notifyStatus, arguments: SpeechToTextStatus.listening.rawValue ) diff --git a/speech_to_text/pubspec.yaml b/speech_to_text/pubspec.yaml index df87827..caafbe9 100644 --- a/speech_to_text/pubspec.yaml +++ b/speech_to_text/pubspec.yaml @@ -1,6 +1,6 @@ name: speech_to_text description: A Flutter plugin that exposes device specific speech to text recognition capability. -version: 6.6.1 +version: 6.6.2 homepage: https://github.com/csdcorp/speech_to_text environment: