首页 > 解决方案 > Flutter : 处理语音.OnError 颤动语音

问题描述

我有带有语音识别功能的应用程序,我正在使用Flutter Speech Package。一切都很好,我可以聆听、停止和取消语音。但问题是,我无法处理这个包给出的错误。

在我的控制台中,我可以看到如下错误消息的数量:

I/flutter (21660): _platformCallHandler call speech.onSpeechAvailability true
I/flutter (21660): _platformCallHandler call speech.onSpeechAvailability false
I/flutter (21660): _platformCallHandler call speech.onError 4 <================
I/flutter (21660): _platformCallHandler call speech.onSpeechAvailability true
I/flutter (21660): _platformCallHandler call speech.onRecognitionStarted null
I/flutter (21660): _platformCallHandler call speech.onSpeech
I/flutter (21660): _platformCallHandler call speech.onSpeechAvailability false
I/flutter (21660): _platformCallHandler call speech.onError 7 <================

包示例中,已经给出了方法_speech.setErrorHandler(),我想根据错误号显示不同的消息错误。我怎样才能做到这一点 ?

到目前为止,对于所有类型的错误,我只显示 1 条消息错误。

我的源代码:


class FabVoiceCustom extends StatefulWidget {
  @override
  _FabVoiceCustomState createState() => _FabVoiceCustomState();
}

//TODO Munculkan Tombol Stop Ketika Listen = True
class _FabVoiceCustomState extends State<FabVoiceCustom> {
  SpeechRecognition _speechRecognition;
  bool _isListening = false;
  bool _isAvailable = false;
  String resultText = "";
  final TaskProvider taskProvider = TaskProvider();
  @override
  void initState() {
    super.initState();
    initSpeechRecognizer();
  }

  void initSpeechRecognizer() {
    _speechRecognition = SpeechRecognition();

    _speechRecognition.setAvailabilityHandler(
        (bool result) => setState(() => _isAvailable = result));

    _speechRecognition.setRecognitionStartedHandler(
        () => setState(() => _isListening = true));

    _speechRecognition.setRecognitionResultHandler(
        (String speech) => setState(() => resultText = speech));

    _speechRecognition.setRecognitionCompleteHandler((String speech) {
      setState(() => _isListening = false);
      BotToast.showSimpleNotification(title: resultText);
      taskProvider.addTask(
          taskModel: TaskModel()
            ..idTask = DateTime.now()
            ..titleTask = resultText
            ..imageTask = null
            ..dateCreate = DateTime.now()
            ..codeIcon = -1);
    });

    _speechRecognition.setErrorHandler(() {
      BotToast.showText(text: "Something Wrong. Try Again");
      initSpeechRecognizer();
      setState(() {
        _isListening = false;
      });
    });

    _speechRecognition
        .activate("en_US")
        .then((result) => setState(() => _isAvailable = result));
  }

  @override
  Widget build(BuildContext context) {
    final mqHeight = MediaQuery.of(context).size.height;
    return WatchBoxBuilder(
      box: Hive.box("language_box"),
      builder: (ctx, box) {
        var isEngland = box.get("isEngland", defaultValue: true);
        return FloatingActionButton.extended(
          clipBehavior: Clip.antiAlias,
          label: Text(_isListening ? " Stop" : "Say It"),
          icon: Image.asset(
            _isListening
                ? "assets/images/listen.png"
                : "assets/images/voice.png",
            width: mqHeight / 18,
          ),
          onPressed: _isListening
              ? () {
                  if (_isListening) {
                    _speechRecognition.stop().then((result) => setState(() {
                          _isListening = result;
                          print("result Stop : $result");
                        }));
                  }
                }
              : () {
                  if (_isAvailable && !_isListening) {
                    _speechRecognition
                        .activate(isEngland ? "en_US" : "id_ID")
                        .then((_) => _speechRecognition.listen());
                  }
                },
        );
      },
    );
  }
}

标签: flutterdart

解决方案


对于 onError 4 ====> 服务器发送错误状态。对于 onError 7 ====> 没有匹配的识别结果。

另外,您可以查看文档,它将对您有所帮助。====> https://developer.android.com/reference/android/speech/SpeechRecognizer.html


推荐阅读