Skip to content

Instantly share code, notes, and snippets.

@jtmuller5
Created April 15, 2021 01:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jtmuller5/7a20ac9466af8699d519b0e8ae47d03d to your computer and use it in GitHub Desktop.
Save jtmuller5/7a20ac9466af8699d519b0e8ae47d03d to your computer and use it in GitHub Desktop.
import 'package:injectable/injectable.dart';
import 'package:speech_to_text/speech_recognition_result.dart';
import 'package:speech_to_text/speech_to_text.dart' as stt;
@singleton
class SpeechService{
bool speechAvailable = false;
stt.SpeechToText speech = stt.SpeechToText();
/// Initialization function called when the app is first opened
void initializeSpeechService() async {
speechAvailable = await speech.initialize( onStatus: (status) {
print('Speech to text status: '+ status);
}, onError: (errorNotification) {
print('Speech to text error: '+ errorNotification.errorMsg);
}, );
}
/// Start listening for user input
/// resultCallback is specified by the caller
void startListening(Function(SpeechRecognitionResult result) resultCallback){
speech.listen(onResult: resultCallback,);
}
/// Stop listening to user input and free up the audio stream
Future<void> stopListening() async {
await speech.stop();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment