如何处理_platformCallHandler呼叫语音错误 7?



我正在尝试在颤振中实现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;
}
speech_recognition

不主动维护。还有很多拉取请求仍在等待合并。您可以更喜欢speech_to_text库。

default案例之前的_platformCallHandler函数中添加以下大小写。

case "speech.onError":
errorHandler();
break;

在下面声明errorHandlerrecognitionCompleteHandler

VoidCallback errorHandler;

在文件末尾,声明用于设置此errorHandler的公共方法

void setErrorHandler(VoidCallback handler) => errorHandler = handler;

这是很多工作,因此您可以使用已经实现上述功能的flutter_speech库。

在实现中,处理错误处理程序

_speechRecognition.setErrorHandler(() {
initSpeechRecognizer();
});

最新更新