如何使语音识别在Flutter Web上工作

e3bfsja2  于 2023-05-30  发布在  Flutter
关注(0)|答案(1)|浏览(189)

我正在用flutter开发一款语音识别应用,它可以在ios和android上运行,但不能在网络上运行。所以我决定在web文件夹中添加一个app.js文件,这样我就可以在dart文件中使用它。我能够让这个文件中的其他函数工作,但每当我调用应该使网络语音识别工作的函数时,它都不工作,我在控制台中得到一些错误消息。下面是app.js文件中的代码:

let container = document.createElement('div');
let paragraph = document.createElement('p');
container.appendChild(paragraph);
document.body.appendChild(container);

window.SpeechRecognition = webkitSpeechRecognition || window.SpeechRecognition;
const synth = window.speechSynthesis;
recognition = new SpeechRecognition();
recognition.interimResults = true;

function speak(action) {
    const utterThis = new SpeechSynthesisUtterance(action);
    synth.speak(utterThis);
  };

function dictate() {
    recognition.start();
    recognition.onresult = (event) => {
        console.log(event);
        const speechToText = event.results[0][0].transcript;
        console.log(speechToText);
        paragraph.textContent = speechToText;
        
        if (event.results[0].isFinal) {
            paragraph = document.createElement('p');
            container.appendChild(paragraph);

            speak('You are welcomed');
        }
    };
    };

window.logger = (flutter_value) => {
   console.log({ js_context: this, flutter_value });
}

在我的main.dart文件中,我使用了import 'dart:js' as js;,这样我就可以像这样调用函数

void _incrementCounter() {
    setState(() {
      js.context
          .callMethod('dictate');
    });
  }

当我像这样调用其他函数时,它工作正常,但当我调用dictate函数时,它不工作,我在控制台中得到以下消息:

404, net::ERR_HTTP_RESPONSE_CODE_FAILURE
js_primitives.dart:32 ══╡ EXCEPTION CAUGHT BY GESTURE ╞═══════════════════════════════════════════════════════════════════
js_primitives.dart:32 The following DomException object was thrown while handling a gesture:
js_primitives.dart:32   InvalidStateError: Failed to execute 'start' on 'SpeechRecognition': recognition has already
js_primitives.dart:32 started.
js_primitives.dart:32 
js_primitives.dart:32 When the exception was thrown, this was the stack:
js_primitives.dart:32 app.js 28:17                                                                                                                                                           dictate
js_primitives.dart:32 package:build_web_compilers/src/dev_compiler/C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/_internal/js_dev_runtime/lib/js/dart2js/js_dart2js.dart 258:53  callMethod
js_primitives.dart:32 package:flutter_js/main.dart 50:12                                                                                                                                     <fn>
js_primitives.dart:32 package:flutter/src/widgets/framework.dart 1148:30                                                                                                                     setState
js_primitives.dart:32 package:flutter_js/main.dart 48:5                                                                                                                                      [_incrementCounter]
js_primitives.dart:32 package:flutter/src/material/ink_well.dart 706:14                                                                                                                      [_handleTap]
js_primitives.dart:32 package:flutter/src/material/ink_well.dart 789:36                                                                                                                      <fn>
js_primitives.dart:32 package:flutter/src/gestures/recognizer.dart 182:24                                                                                                                    invokeCallback
js_primitives.dart:32 package:flutter/src/gestures/tap.dart 486:47                                                                                                                           handleTapUp
js_primitives.dart:32 package:flutter/src/gestures/tap.dart 264:5                                                                                                                            [_checkUp]
js_primitives.dart:32 package:flutter/src/gestures/tap.dart 236:7                                                                                                                            acceptGesture
js_primitives.dart:32 package:flutter/src/gestures/arena.dart 156:12                                                                                                                         sweep
js_primitives.dart:32 package:flutter/src/gestures/binding.dart 222:20                                                                                                                       handleEvent
js_primitives.dart:32 package:flutter/src/gestures/binding.dart 198:14                                                                                                                       dispatchEvent
js_primitives.dart:32 package:flutter/src/gestures/binding.dart 156:7                                                                                                                        [_handlePointerEvent]
js_primitives.dart:32 package:flutter/src/gestures/binding.dart 102:7                                                                                                                        [_flushPointerEventQueue]
js_primitives.dart:32 package:flutter/src/gestures/binding.dart 86:32                                                                                                                        [_handlePointerDataPacket]
js_primitives.dart:32 package:build_web_compilers/src/dev_compiler/C:/b/s/w/ir/cache/builder/src/out/host_debug/flutter_web_sdk/lib/_engine/engine/pointer_binding.dart 83:15                [_onPointerData]
js_primitives.dart:32 package:build_web_compilers/src/dev_compiler/C:/b/s/w/ir/cache/builder/src/out/host_debug/flutter_web_sdk/lib/_engine/engine/pointer_binding.dart 334:7                <fn>
js_primitives.dart:32 package:build_web_compilers/src/dev_compiler/C:/b/s/w/ir/cache/builder/src/out/host_debug/flutter_web_sdk/lib/_engine/engine/pointer_binding.dart 183:16

是不是有什么地方出错了,或者我有什么替代方案可以让语音识别为flutter web工作。先谢谢你了!

w7t8yxp5

w7t8yxp51#

无论这个问题是什么(两年前),现在都不是真的。
我刚刚使用speech_to_text包将语音转文本功能添加到我的应用程序中,它可以在Chrome中工作,无需任何特殊努力。

相关问题