Flutter Speech Recognition
Target Audience: Beginner
Plugin: speech_recognition
A flutter plugin to use the speech recognition on iOS and Android
Adding Permission
Android :
<uses-permission android:name="android.permission.RECORD_AUDIO" />
iOS
Add Info.plist
:
- Privacy — Microphone Usage Description
- Privacy — Speech Recognition Usage Description
<key>NSMicrophoneUsageDescription</key>
<string>This application needs to access your microphone</string>
<key>NSSpeechRecognitionUsageDescription</key>
<string>This application needs the speech recognition permission</string>
Defining Variables :
_isAvailable : tell the platform is Android or ios is available to interact with it or not
_isListening: tell us whether or not the app is currently listening to the microphone
resultText: in this, we set our final text that comes from our speech
SpeechRecognition _speechRecognition;
bool _isAvailable = false;
bool _isListening = false;
String resultText = "";
Initialize it
First, we need to initialize some bunch of different callback for the speech recognition object to work everything properly
setAvailabilityHandler: which let us know about availability and here we update our _isAvailable variable.
setRecognitionStartedHandler: It starts executed when we start the speech recognition service. when its start working we set our _isListening true.
setRecognitionResultHandler: This is our main through this callBack we get out text from our speech recognition service. And here we set our resultText
setRecognitionCompleteHandler: It starts executed when we end with our speech recognition service. when its end up we set our _isListening false.
_speechRecognition = SpeechRecognition();
_speechRecognition.setAvailabilityHandler(
(bool result) => setState(() => _isAvailable = result),
);
_speechRecognition.setRecognitionStartedHandler(
() => setState(() => _isListening = true),
);
_speechRecognition.setRecognitionResultHandler(
(String speech) => setState(() => resultText = speech),
);
_speechRecognition.setRecognitionCompleteHandler(
() => setState(() => _isListening = false),
);
_speechRecognition.activate().then(
(result) => setState(() => _isAvailable = result),
);
Listening Speech Recognition Service
if (_isListening)
_speechRecognition.stop().then(
(result) => setState(() => _isListening = result),
);
OnStop
if (_isListening)
_speechRecognition.stop().then(
(result) => setState(() => _isListening = result),
);
OnCanceling
if (_isListening)
_speechRecognition.cancel().then(
(result) => setState(() {
_isListening = result;
resultText = "";
}),
);
Don’t forget to give permission
Link for repository
flutter-devs/speech-Recognition-demo
speech_recognition plugin exploring This project is a starting point for a Flutter application. A few resources to get…github.com
Thanks for reading this article if you find anything that could be improved please let me know, I would love to improve.
Feel free to connect with us:
And read more articles from FlutterDevs.com
FlutterDevs team of Flutter developers to build high-quality and functionally-rich apps. Hire flutter developer for your cross-platform Flutter mobile app project on an hourly or full-time basis as per your requirement! You can connect with us on Facebook, GitHub, Twitter and linkedIn for any flutter related queries.
We welcome feedback, and hope that you share what you’re working on using #FlutterDevs. We truly enjoy seeing how you use Flutter to build beautiful, interactive web experiences!.