我正在尝试在flutter中实现speech_recognition插件。当我说话的时候,识别效果很好,应用程序运行起来也很流畅。但是,当我点击麦克风按钮,什么也没说时,它显示以下错误,然后麦克风按钮的功能停止,直到我重新启动应用程序。
D/SpeechRecognitionPlugin( 1155): onError : 7
I/flutter ( 1155): _platformCallHandler call speech.onSpeechAvailability false
I/flutter ( 1155): _platformCallHandler call speech.onError 7
I/flutter ( 1155): Unknowm method speech.onError 有人能帮我解决这个问题吗?
这是我的speech_recognition.dart文件
import 'dart:async';
import 'dart:ui';
import 'package:flutter/services.dart';
typedef void AvailabilityHandler(bool result);
typedef void StringResultHandler(String text);
/// the channel to control the speech recognition
class SpeechRecognition {
static const MethodChannel _channel =
const MethodChannel('speech_recognition');
static final SpeechRecognition _speech = new SpeechRecognition._internal();
factory SpeechRecognition() => _speech;
SpeechRecognition._internal() {
_channel.setMethodCallHandler(_platformCallHandler);
}
AvailabilityHandler availabilityHandler;
StringResultHandler currentLocaleHandler;
StringResultHandler recognitionResultHandler;
VoidCallback recognitionStartedHandler;
VoidCallback recognitionCompleteHandler;
VoidCallback errorHandler;
/// ask for speech recognizer permission
Future activate() => _channel.invokeMethod("speech.activate");
/// start listening
Future listen({String locale}) =>
_channel.invokeMethod("speech.listen", locale);
Future cancel() => _channel.invokeMethod("speech.cancel");
Future stop() => _channel.invokeMethod("speech.stop");
Future _platformCallHandler(MethodCall call) async {
print("_platformCallHandler call ${call.method} ${call.arguments}");
switch (call.method) {
case "speech.onSpeechAvailability":
availabilityHandler(call.arguments);
break;
case "speech.onCurrentLocale":
currentLocaleHandler(call.arguments);
break;
case "speech.onSpeech":
recognitionResultHandler(call.arguments);
break;
case "speech.onRecognitionStarted":
recognitionStartedHandler();
break;
case "speech.onRecognitionComplete":
recognitionCompleteHandler();
break;
case "speech.onError":
errorHandler();
break;
default:
print('Unknowm method ${call.method} ');
}
}
// define a method to handle availability / permission result
void setAvailabilityHandler(AvailabilityHandler handler) =>
availabilityHandler = handler;
// define a method to handle recognition result
void setRecognitionResultHandler(StringResultHandler handler) =>
recognitionResultHandler = handler;
// define a method to handle native call
void setRecognitionStartedHandler(VoidCallback handler) =>
recognitionStartedHandler = handler;
// define a method to handle native call
void setRecognitionCompleteHandler(VoidCallback handler) =>
recognitionCompleteHandler = handler;
void setCurrentLocaleHandler(StringResultHandler handler) =>
currentLocaleHandler = handler;
void setErrorHandler(VoidCallback handler) => errorHandler = handler;
}发布于 2020-06-20 01:24:52
speech_recognition不是主动维护的。有如此多的拉取请求仍在等待合并。你可以选择speech_to_text库。
在default用例之前的_platformCallHandler函数中,添加以下用例。
case "speech.onError":
errorHandler();
break;在recognitionCompleteHandler下面声明errorHandler。
VoidCallback errorHandler;在文件的末尾,声明公共方法以设置此errorHandler
void setErrorHandler(VoidCallback handler) => errorHandler = handler;这是大量的工作,所以您可以使用已经实现了上述功能的flutter_speech库。
在您的实现中,处理错误处理程序
_speechRecognition.setErrorHandler(() {
initSpeechRecognizer();
});https://stackoverflow.com/questions/62475277
复制相似问题