我正在使用颤振构建一个适用于 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');
});
}
当我像这样调用其他函数时,它可以正常工作,但是当我调用指令函数时它不起作用,并且我在控制台中收到此消息:
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
是否有什么问题,或者我必须有什么替代方案才能让语音识别适用于颤振网络。提前致谢!