HarmonyOS鸿蒙Next中在Pura70 Pro+ 、Mate60 Pro+等性能较好的设备上使用小艺输入法语音输入,inputMethod.getController().on('insertText', 接口返回的文字会叠加输出。
HarmonyOS鸿蒙Next中在Pura70 Pro+ 、Mate60 Pro+等性能较好的设备上使用小艺输入法语音输入,inputMethod.getController().on(‘insertText’, 接口返回的文字会叠加输出。
导入 { inputMethod } from “@kit.IMEKit”;
const inputMethodController = inputMethod.getController();
inputMethodController.on(‘insertText’, (text: string) => { console.log(‘insertText’, text); });
使用小艺输入法的语音输入:测试语音输入
insertText 监听函数被持续调用并持续返回:“测”,“测试”,“测试语”,“测试语音”, “测试语音输”,“测试语音输入”
此种行为导致业务调用出现问题,会把 insertText 的输出全部显示到用户端:“测测试测试语测试语音测试语音输测试语音输入”,但是用户只是使用语音输入了:“测试语音输入”
更多关于HarmonyOS鸿蒙Next中在Pura70 Pro+ 、Mate60 Pro+等性能较好的设备上使用小艺输入法语音输入,inputMethod.getController().on('insertText', 接口返回的文字会叠加输出。的实战教程也可以访问 https://www.itying.com/category-93-b0.html
我不知道行不行,你试试。
更多关于HarmonyOS鸿蒙Next中在Pura70 Pro+ 、Mate60 Pro+等性能较好的设备上使用小艺输入法语音输入,inputMethod.getController().on('insertText', 接口返回的文字会叠加输出。的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
不行,怎么还有 dom 操作,是 ai 生成的吧。。。
let inputMethod = getInputMethodObject(); let firstInput = true;
inputMethod.getController().on(‘insertText’, function(text) { let isAppendMode = checkInputMethodSetting(‘appendMode’); if (isAppendMode) { if (!firstInput) { let inputBox = document.getElementById(‘inputBox’); inputBox.value = ‘’; } } let inputBox = document.getElementById(‘inputBox’); inputBox.value = text; firstInput = false; });
function checkInputMethodSetting(settingKey) { return false; }
function getInputMethodObject() { return {}; } 语言:JavaScript
在HarmonyOS鸿蒙Next中,小艺输入法语音输入出现文字叠加输出的问题是由于inputMethod.getController().on('insertText')
回调重复触发导致的。解决方法:
- 在回调处理逻辑中加入防抖机制
- 检查是否多次注册了同一事件监听
- 确保每次语音识别结果处理前清除前次缓存
关键代码示例:
let isProcessing = false;
inputMethod.getController().on('insertText', (text) => {
if (!isProcessing) {
isProcessing = true;
// 处理文本插入
isProcessing = false;
}
});
```,
这是一个典型的语音输入增量更新问题。在HarmonyOS Next中,小艺输入法的语音识别采用了流式处理机制,会实时返回部分识别结果。建议采用以下解决方案:
- 使用去重逻辑处理增量文本:
let lastText = '';
inputMethodController.on('insertText', (text: string) => {
if (!text.startsWith(lastText)) {
// 新语句开始,重置缓存
lastText = text;
} else {
// 只处理新增部分
const newText = text.slice(lastText.length);
lastText = text;
text = newText;
}
console.log('processed text:', text);
});
- 或者等待最终识别结果(约1秒无新回调视为结束):
let timer: number | null = null;
let finalText = '';
inputMethodController.on('insertText', (text: string) => {
finalText = text;
if (timer) {
clearTimeout(timer);
}
timer = setTimeout(() => {
console.log('final text:', finalText);
timer = null;
}, 1000) as unknown as number;
});
这种设计是语音输入功能的正常行为,开发者需要根据业务场景选择合适的处理方式。