flutter - 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());
}
},
);
},
);
}
}
解决方案
对于 onError 4 ====> 服务器发送错误状态。对于 onError 7 ====> 没有匹配的识别结果。
另外,您可以查看文档,它将对您有所帮助。====> https://developer.android.com/reference/android/speech/SpeechRecognizer.html
推荐阅读
- python - 如何导入文件夹中的所有脚本并将它们用作函数?
- javascript - 如何控制 HTML 页面在 GET 请求中返回的内容
- php - 对 Laravel Livewire 组件进行水合和脱水意味着什么?
- flutter - Flutter:如何更改 AppBar Title 字体?
- amazon-web-services - AWS Cloudformation:重用 Fn::FindInMap 的结果
- python - Pipenv 无法识别 PyEnv python 安装的垫片
- python - 如何修复此 python NameError?
- javascript - scrollIntoView 在反应 useEffect 中无法在移动设备上运行
- node.js - 如何渲染动态导入的反应组件?
- libreoffice - Libreoffice Base - Basic - 如何从 csv 文件中获取数据并放入 odb 数据库?