Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PR to fix issue with first word missed on iOS on 2nd+ calls to listen… #513

Merged
merged 1 commit into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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