首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何处理_platformCallHandler call speech.onError 7?

如何处理_platformCallHandler call speech.onError 7?
EN

Stack Overflow用户
提问于 2020-06-20 01:06:15
回答 1查看 165关注 0票数 0

我正在尝试在flutter中实现speech_recognition插件。当我说话的时候,识别效果很好,应用程序运行起来也很流畅。但是,当我点击麦克风按钮,什么也没说时,它显示以下错误,然后麦克风按钮的功能停止,直到我重新启动应用程序。

代码语言:javascript
复制
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文件

代码语言:javascript
复制
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;

}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-06-20 01:24:52

speech_recognition不是主动维护的。有如此多的拉取请求仍在等待合并。你可以选择speech_to_text库。

default用例之前的_platformCallHandler函数中,添加以下用例。

代码语言:javascript
复制
case "speech.onError":
   errorHandler();
   break;

recognitionCompleteHandler下面声明errorHandler

代码语言:javascript
复制
VoidCallback errorHandler;

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

代码语言:javascript
复制
void setErrorHandler(VoidCallback handler) => errorHandler = handler;

这是大量的工作,所以您可以使用已经实现了上述功能的flutter_speech库。

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

代码语言:javascript
复制
_speechRecognition.setErrorHandler(() {
   initSpeechRecognizer();
});
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62475277

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档