Created
April 15, 2021 01:38
-
-
Save jtmuller5/7a20ac9466af8699d519b0e8ae47d03d to your computer and use it in GitHub Desktop.
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: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