Как обработать _platformCallHandler call speech.onError 7? - PullRequest
0 голосов
/ 19 июня 2020

Я пытаюсь реализовать плагин Speech_recognition во флаттере. И распознавание работает отлично, когда я говорю, и приложение работает без сбоев. но когда я нажимаю кнопку mi c и ничего не говорю, появляется следующая ошибка, а затем функциональность кнопки mi c останавливается, пока я не перезапущу приложение.

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;

}

1 Ответ

1 голос
/ 19 июня 2020

speech_recognition активно не поддерживается. Так много запросов на вытягивание все еще ожидают объединения. Вы можете предпочесть библиотеку speech_to_text .

В функции _platformCallHandler перед случаем default добавьте регистр ниже.

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

Объявить errorHandler ниже recognitionCompleteHandler.

VoidCallback errorHandler;

В конце файла объявите метод publi c, чтобы установить этот errorHandler

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

Это большая работа, поэтому вы можете используйте библиотеку flutter_speech , в которой уже реализованы указанные выше функции.

В вашей реализации обработайте обработчик ошибок

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