Created
May 22, 2020 01:34
-
-
Save vallamost/3d3b10ef60c5bb994ae0d9bbcf09050d to your computer and use it in GitHub Desktop.
VoiceFunctions provider
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/cupertino.dart'; | |
import 'package:speech_to_text/speech_recognition_error.dart'; | |
import 'package:speech_to_text/speech_recognition_result.dart'; | |
import 'package:speech_to_text/speech_to_text.dart'; | |
class VoiceFunctions extends ChangeNotifier { | |
bool speechEngineState = false; | |
final speechTimeoutDuration = Duration(seconds: 5); | |
double level = 0.0; | |
String lastWords = ""; | |
String lastError = ""; | |
String lastStatus = ""; | |
String secondVoiceInputTest = ""; | |
String _currentLocaleId = ""; | |
List<LocaleName> _localeNames = []; | |
final SpeechToText speechController = SpeechToText(); | |
String verifyAnswerWords = ''; | |
Future<void> initSpeechState() async { | |
bool speechEngineState = await speechController.initialize(onError: errorListener, onStatus: statusListener); | |
try { | |
if (speechEngineState == true) { | |
print("getting locales.."); | |
_localeNames = await speechController.locales(); | |
var systemLocale = await speechController.systemLocale(); | |
_currentLocaleId = systemLocale.localeId; | |
print("Speech engine initialized!"); | |
} | |
if (speechEngineState == false || speechEngineState == null) { | |
throw "Unable to initalize speechController engine, check microphone permissions."; | |
} | |
} catch (exception) { | |
print(exception); | |
} | |
} | |
void stopListening() { | |
speechController.stop().then((_) { | |
print("stopping.."); | |
// setState(() { | |
// level = 0.0; | |
// lastStatus = "Stopped, not listening."; | |
// }); | |
}); | |
} | |
void cancelListening() { | |
speechController.cancel().then((value) { | |
// setState(() { | |
// level = 0.0; | |
// lastStatus = "Not Listening"; | |
// }); | |
}); | |
} | |
void errorListener(SpeechRecognitionError error) { | |
print("error: " + error.toString()); | |
if (error.errorMsg.contains('error_speech_timeout')) { | |
print("time out occurred"); | |
stopListening(); | |
// setState(() { | |
// lastStatus = "speechController timeout hit, ready."; | |
// }); | |
} | |
if (error.errorMsg.contains('error_no_match')) { | |
print("No match found for voice sample received."); | |
stopListening(); | |
// setState(() { | |
// lastStatus = "Couldn't find any words in voice sample.\n Please try again.."; | |
// }); | |
} else { | |
print("error: " + error.toString()); | |
stopListening(); | |
// setState(() { | |
// lastError = "${error.errorMsg} - ${error.permanent}"; | |
// }); | |
} | |
} | |
void statusListener(String status) { | |
print(status); | |
// setState(() { | |
// var speechEngineStatus = "$status"; | |
// // lastStatus = "$status"; | |
// }); | |
} | |
_switchLang(selectedVal) { | |
// setState(() { | |
// _currentLocaleId = selectedVal; | |
// }); | |
print(selectedVal); | |
} | |
void soundLevelListener(double level) { | |
// setState(() { | |
// this.level = level; | |
// }); | |
} | |
// First Listener | |
void startFirstVoiceListener() { | |
lastWords = ""; | |
lastError = ""; | |
speechController.listen( | |
onResult: firstVoiceListenerResult, | |
listenFor: speechTimeoutDuration, | |
localeId: _currentLocaleId, | |
onSoundLevelChange: soundLevelListener, | |
cancelOnError: true, | |
partialResults: true); | |
// setState(() { | |
// lastStatus = "Listening..."; | |
// }); | |
} | |
void firstVoiceListenerResult(SpeechRecognitionResult result) { | |
print("${result.recognizedWords} - ${result.finalResult}"); | |
// print(result.confidence); | |
print(result); | |
verifyAnswerWords = result.recognizedWords; | |
lastWords = result.recognizedWords; | |
notifyListeners(); | |
// setState(() { | |
// // lastWords = "${result.recognizedWords} - ${result.finalResult}"; | |
// _textInputcontroller.text = "${result.recognizedWords}"; | |
// lastStatus = "Words found from your voice. Please review them. Ready."; | |
// }); | |
} | |
void getFirstVoiceResult() => lastWords; | |
// Second Listener | |
void startSecondListener(Function resultListener) { | |
speechController.listen( | |
onResult: resultListener, | |
listenFor: speechTimeoutDuration, | |
localeId: _currentLocaleId, | |
onSoundLevelChange: soundLevelListener, | |
cancelOnError: true, | |
partialResults: true); | |
// setState(() { | |
// lastStatus = "Listening..."; | |
// }); | |
} | |
// No results are provided from this | |
void mySecondResultListener(SpeechRecognitionResult result) { | |
print(result.confidence); // Nothing printed | |
print("${result.recognizedWords} - ${result.finalResult}"); | |
// setState(() { | |
// _textVerifyInputcontroller.text = "${result.recognizedWords}"; | |
// lastStatus = "Not listening"; | |
// // secondVoiceInputTest = "${result.recognizedWords} - ${result.finalResult}"; | |
// }); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment