Skip to content

Commit

Permalink
PR to fix issue with first word missed on iOS on 2nd+ calls to listen…
Browse files Browse the repository at this point in the history
…ForSpeech (#513)
  • Loading branch information
vongrad committed May 22, 2024
1 parent 420008a commit d4e1edd
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
28 changes: 17 additions & 11 deletions speech_to_text/ios/Classes/SwiftSpeechToTextPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 ) {
Expand Down Expand Up @@ -316,7 +320,7 @@ public class SwiftSpeechToTextPlugin: NSObject, FlutterPlugin {
stopAllPlayers()
do {
try trap {
self.audioEngine.stop()
self.audioEngine?.stop()
}
}
catch {
Expand Down Expand Up @@ -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.")
}
Expand Down Expand Up @@ -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 )
Expand Down
2 changes: 1 addition & 1 deletion speech_to_text/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down

0 comments on commit d4e1edd

Please sign in to comment.