フラッターを使用して、ios と android で動作する音声認識アプリを構築していますが、Web では動作しません。そこで、dart ファイルで使用できるように、app.js ファイルを web フォルダーに追加することにしました。このファイルの他の機能を動作させることはできますが、Web の音声認識を機能させるはずの機能を呼び出すたびに機能せず、コンソールにエラー メッセージが表示されます。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
何か問題がありますか、またはフラッター Web で音声認識を機能させるために必要な代替手段はありますか。前もって感謝します!