rxlabz / speech_recognition

A Flutter plugin to use speech recognition on iOS & Android (Swift/Java)
https://pub.dartlang.org/packages/speech_recognition
Other
336 stars 197 forks source link

Flutter Android PIXEL Emulator - _platformCallHandler call speech.onError 2 #9

Open vinayakvanarse opened 6 years ago

vinayakvanarse commented 6 years ago

Hi,

First of all thanks for the utility. It works perfect on iOS. I am having issues with Android Emulator (android-x86). When I start the emulator and then run flutter run I don't see any errors. It loads the speech.dart implementation without any errors. It does get the call speech.onSpeechAvailability true.

Now when I press Listen it throws error

D/SpeechRecognitionPlugin(10082): onError : 2 I/flutter (10082): _platformCallHandler call speech.onSpeechAvailability false I/flutter (10082): _platformCallHandler call speech.onError 2

Here is the full trace from flutter run

Launching lib/main.dart on Android SDK built for x86 in debug mode... registerResGeneratingTask is deprecated, use registerGeneratedFolders(FileCollection) registerResGeneratingTask is deprecated, use registerGeneratedFolders(FileCollection) registerResGeneratingTask is deprecated, use registerGeneratedFolders(FileCollection) Built build/app/outputs/apk/debug/app-debug.apk. I/FlutterActivityDelegate(10082): onResume setting current activity to this I/Choreographer(10082): Skipped 56 frames! The application may be doing too much work on its main thread. D/EGL_emulation(10082): eglMakeCurrent: 0xe2885480: ver 3 0 (tinfo 0xe2883440) I/OpenGLRenderer(10082): Davey! duration=1230ms; Flags=1, IntendedVsync=2987461107766, Vsync=2988394441062, OldestInputEvent=9223372036854775807, NewestInputEvent=0, HandleInputStart=2988406485133, AnimationStart=2988406770133, PerformTraversalsStart=2988406789133, DrawStart=2988520121133, SyncQueued=2988522026133, SyncStart=2988526756133, IssueDrawCommandsStart=2988526931133, SwapBuffers=2988578172133, FrameCompleted=2988696000133, DequeueBufferDuration=38146000, QueueBufferDuration=10954000, D/ (10082): HostConnection::get() New Host Connection established 0xe2899940, tid 10109 D/EGL_emulation(10082): eglMakeCurrent: 0xceab0960: ver 3 0 (tinfo 0xe2883330) I/flutter (10082): _SpeechBotState.activateSpeechRecognizer... D/SpeechRecognitionPlugin(10082): Current Locale : en_US I/flutter (10082): _platformCallHandler call speech.onCurrentLocale en_US I/flutter (10082): Your currentLocale is en_US I/flutter (10082): _SpeechBotState.start => result true D/SpeechRecognitionPlugin(10082): onRmsChanged : -2.12 D/SpeechRecognitionPlugin(10082): onRmsChanged : -2.12 D/SpeechRecognitionPlugin(10082): onReadyForSpeech I/flutter (10082): _platformCallHandler call speech.onSpeechAvailability true D/SpeechRecognitionPlugin(10082): onRmsChanged : -2.12 D/SpeechRecognitionPlugin(10082): onRmsChanged : -2.0 D/SpeechRecognitionPlugin(10082): onRmsChanged : -2.0 D/SpeechRecognitionPlugin(10082): onRmsChanged : -2.0 D/SpeechRecognitionPlugin(10082): onRmsChanged : -2.0 D/SpeechRecognitionPlugin(10082): onRmsChanged : -2.0 D/SpeechRecognitionPlugin(10082): onRmsChanged : -2.0 D/SpeechRecognitionPlugin(10082): onRmsChanged : -2.12 D/SpeechRecognitionPlugin(10082): onError : 2 I/flutter (10082): _platformCallHandler call speech.onSpeechAvailability false I/flutter (10082): _platformCallHandler call speech.onError 2 I/flutter (10082): Unknowm method speech.onError Application finished. (<-- I stopped it)

in AndroidManifest.xml I have included

I have also opened the app settings in Android settings and provided permission to the app to camera, speaker, microphone.

What does speech.onError 2 mean? and how to fix it in emulator?

Thanks

vinayakvanarse commented 6 years ago

Looks like after google search I found Error code 2 means Network_error. Still not sure how to fix it in android-emulator x86 in Flutter.

vinayakvanarse commented 6 years ago

And here is the complete code file Flutter

import 'package:flutter/material.dart'; import 'package:speech_recognition/speech_recognition.dart'; import 'package:tts/tts.dart';

class SpeechBot extends StatefulWidget { @override _SpeechBotState createState() => new _SpeechBotState(); }

class _SpeechBotState extends State { SpeechRecognition _speech;

bool _speechRecognitionAvailable = false; bool _isListening = false;

String transcription = '';

String _currentLocale = 'en_US';

@override initState() { super.initState(); activateSpeechRecognizer(); }

// Platform messages are asynchronous, so we initialize in an async method. void activateSpeechRecognizer() { print('_SpeechBotState.activateSpeechRecognizer... '); _speech = new SpeechRecognition(); _speech.setAvailabilityHandler(onSpeechAvailability); _speech.setCurrentLocaleHandler(onCurrentLocale); _speech.setRecognitionStartedHandler(onRecognitionStarted); _speech.setRecognitionResultHandler(onRecognitionResult); _speech.setRecognitionCompleteHandler(onRecognitionComplete); _speech .activate() .then((res) => setState(() => _speechRecognitionAvailable = res)); }

@override Widget build(BuildContext context) { return new MaterialApp( home: new Scaffold( appBar: new AppBar( title: new Text('SpeechRecognition'), ), body: new Padding( padding: new EdgeInsets.all(8.0), child: new Center( child: new Column( mainAxisSize: MainAxisSize.min, crossAxisAlignment: CrossAxisAlignment.stretch, children: [ new Expanded( child: new Container( padding: const EdgeInsets.all(8.0), color: Colors.grey.shade200, child: new Text(transcription))), _buildButton( onPressed: _speechRecognitionAvailable && !_isListening ? () => start() : null, label: _isListening ? 'Listening...' : 'Listen ($_currentLocale)', ), _buildButton( onPressed: _isListening ? () => cancel() : null, label: 'Cancel', ), _buildButton( onPressed: _isListening ? () => stop() : null, label: 'Stop', ), ], ), )), ), ); }

Widget _buildButton({String label, VoidCallback onPressed}) => new Padding( padding: new EdgeInsets.all(12.0), child: new RaisedButton( color: Colors.cyan.shade600, onPressed: onPressed, child: new Text( label, style: const TextStyle(color: Colors.white), ), ));

void start() => _speech .listen(locale: _currentLocale) .then((result) => print('_SpeechBotState.start => result $result'));

void cancel() => _speech.cancel().then((result) => setState(() { _isListening = result; print('_speech.cancel result is $result'); }));

void stop() => _speech.stop().then((result) => setState(() => _isListening = result));

void onSpeechAvailability(bool result) => setState(() => _speechRecognitionAvailable = result);

void onCurrentLocale(String locale) => setState(() { _currentLocale = locale; print('Your currentLocale is $_currentLocale'); });

void onRecognitionStarted() => setState(() => _isListening = true);

void onRecognitionResult(String text) => setState(() { transcription = text; print('your transcription is $transcription'); });

void onRecognitionComplete() => setState(() { _isListening = false; print('onRecongintionComplete the transcript is $transcription'); Tts.speak(transcription); }); }

mahendraHegde commented 5 years ago

Looks like after google search I found Error code 2 means Network_error. Still not sure how to fix it in android-emulator x86 in Flutter.

Same issue here... Any luck???

mingxun commented 5 years ago

I have the same issue when running on simulator. But it works fine on real device. Hope this helps other people who got the same issue.

toshiossada commented 4 years ago

Same issue here