flutter speech recognition
a flutter plugin to use the speech recognition ios10+ / android 4.1+
installation
- depend on it
add this to your package’s pubspec.yaml file:
dependencies:
speech_recognition: "^0.2.0+1"
- install it
you can install packages from the command line:
$ flutter packages get
alternatively, your editor might support ‘packages get’. check the docs for your editor to learn more.
- import it
now in your dart code, you can use:
import 'package:speech_recognition/speech_recognition.dart';
usage
//..
_speech = new speechrecognition();
// the flutter app not only call methods on the host platform,
// it also needs to receive method calls from host.
_speech.setavailabilityhandler((bool result)
=> setstate(() => _speechrecognitionavailable = result));
// handle device current locale detection
_speech.setcurrentlocalehandler((string locale) =>
setstate(() => _currentlocale = locale));
_speech.setrecognitionstartedhandler(()
=> setstate(() => _islistening = true));
// this handler will be called during recognition.
// the ios api sends intermediate results,
// on my android device, only the final transcription is received
_speech.setrecognitionresulthandler((string text)
=> setstate(() => transcription = text));
_speech.setrecognitioncompletehandler(()
=> setstate(() => _islistening = false));
// 1st launch : speech recognition permission / initialization
_speech
.activate()
.then((res) => setstate(() => _speechrecognitionavailable = res));
//..
speech.listen(locale:_currentlocale).then((result)=> print('result : $result'));
// ...
speech.cancel();
// ||
speech.stop();
recognition
- ios : speech api
- android : speechrecognizer
permissions
ios
infos.plist, add :
- 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>
swift project
this plugin is written in swift, so to use with in a flutter/objc project,
you need to convert the project to “current swift syntax” ( edit/convert/current swift syntax)
android
<uses-permission android:name="android.permission.record_audio" />
limitation
on ios, by default the plugin is configured for french, english, russian, spanish.
on android, without additional installations, it will probably works only with the default device locale.
Comments are closed.