HarmonyOS 鸿蒙Next 语音识别不支持阿拉伯数字
HarmonyOS 鸿蒙Next 语音识别不支持阿拉伯数字
采用实时录音识别,不支持识别阿拉伯数字?这不合理吧
2 回复
import { speechRecognizer } from '@kit.CoreSpeechKit';
import abilityAccessCtrl, { Permissions } from "@ohos.abilityAccessCtrl";
const TAG = 'DEMO'
@Entry
@Component
struct Index {
@State message: string = '输入框语音识别';
@State show: boolean = false
@State voiceText: string = ''
@State voiceLastText: string = ''
arsEngine: speechRecognizer.SpeechRecognitionEngine | null = null
atManager: abilityAccessCtrl.AtManager = abilityAccessCtrl.createAtManager()
sessionId: string = ''
context = getContext()
@Styles pressedStyles():void {
.backgroundColor("#ff00830d")
.outline({ width: 10, color: '#ffb1ffb8', radius: 100 })
}
build() {
Row() {
Column({ space: 10 }) {
Text(this.message).fontSize(30).margin({ bottom: 50 })
Row({ space: 5 }) {
Row () {
Image($rawfile('icon_search.png'))
.size({ width: 18, height: 18 })
.opacity(.5)
.margin({ left: 10, right: 10 })
TextInput({ placeholder: '搜索', text: this.voiceLastText })
.padding({ left: 0 })
.layoutWeight(1)
.backgroundColor(Color.Transparent)
.height('100%')
Column() {
Image($rawfile('icon_microphone.png'))
.size({ width: 20, height: 20 })
}.width(45).height('100%')
.justifyContent(FlexAlign.Center)
.onClick(async () => {
const getPermissionSuccess = await this.getPermission()
if (getPermissionSuccess) {
this.show = true
this.voiceText = ''
}
if (!this.arsEngine) {
await this.createSREngine()
}
})
}
.layoutWeight(1)
.backgroundColor(Color.White)
.borderRadius(5)
Text('搜索')
.margin({ left: 7 })
}.width('100%')
.height(40)
}
.width('100%')
.padding({ top: 20, left: 20, right: 20 })
.height('100%')
// voice底部弹窗
Panel(this.show) {
Column({ space: 15 }) {
Text(this.voiceText)
.height(60)
.padding({ left: 20, right: 20 })
.alignSelf(ItemAlign.Stretch)
Column() {
Text('按住说话')
.fontSize(14)
.fontColor('#999')
}
.height(30)
Stack() {
Image($rawfile('icon_microphone.png'))
.width(25)
}
.size({ width: 50, height: 50 })
.backgroundColor('#a309ff22')
.borderRadius(100)
.outline({ width: 0 })
.stateStyles({
pressed: this.pressedStyles
})
.onTouch(async (event) => {
if (event.type === TouchType.Down) {
this.startListener()
} else if (event.type === TouchType.Up) {
this.finishListener()
}
})
}
.width('100%')
.height('100%')
.justifyContent(FlexAlign.Center)
}
.width('100%')
.type(PanelType.Foldable)
.mode(PanelMode.Half)
.customHeight(PanelHeight.WRAP_CONTENT)
.dragBar(true) // 默认开启
.halfHeight(300) // 默认一半
.showCloseIcon(true) // 显示关闭图标
.position({ x: 0 })
.backgroundColor(Color.White)
.onChange((width: number, height: number, mode: PanelMode) => {
console.log(TAG, `width: ${width}, height: ${height}, mode: ${mode}`)
})
.onHeightChange((height: number) => {
if (height <= 0) {
this.voiceLastText = this.voiceText
}
})
}
.height('100%')
.backgroundColor('#ffe7e7e7')
}
aboutToAppear(): void {
}
aboutToDisappear(): void {
this.arsEngine?.shutdown()
}
async createSREngine () {
const extraParams: Record<string, Object> = {
"locate": "CN",
"recognizerMode": "short"
}
const initParamsInfo: speechRecognizer.CreateEngineParams = {
language: 'zh-CN', // 当前仅支持“zh_CN”中文
online: 1,
extraParams
}
try {
this.arsEngine = await speechRecognizer.createEngine(initParamsInfo)
console.log(TAG, `获取语音转文字示例成功`)
this.setListener()
} catch (e) {
console.error(TAG, `获取语音转文字实例失败 ${e.code} ${e.message}`)
}
}
setListener () {
// 创建回调对象
let setListener: speechRecognizer.RecognitionListener = {
// 开始识别成功回调
onStart: (sessionId: string, eventMessage: string) => {
console.info(TAG, "onStart sessionId: " + sessionId + " eventMessage: " + eventMessage);
},
// 事件回调
onEvent: (sessionId: string, eventCode: number, eventMessage: string) => {
console.info(TAG, "onEvent sessionId: " + sessionId + " eventCode: " + eventCode + "eventMessage: " + eventMessage);
},
// 识别结果回调,包括中间结果和最终结果
onResult: (sessionId: string, result: speechRecognizer.SpeechRecognitionResult) => {
console.info(TAG, "onResult sessionId: " + sessionId + " result: " + JSON.stringify(result));
if (result.result) {
this.voiceText = result.result
}
if (result.isLast) {
setTimeout(() => {
this.show = false
}, 1000)
}
},
// 识别完成回调
onComplete: (sessionId: string, eventMessage: string) => {
console.info(TAG, "onComplete sessionId: " + sessionId + " eventMessage: " + eventMessage);
},
// 错误回调,错误码通过本方法返回
// 返回错误码1002200002,开始识别失败,重复启动startListening方法时触发
// 更多错误码请参考错误码参考
onError: (sessionId: string, errorCode: number, errorMessage: string) => {
console.error(TAG, "onError sessionId: " + sessionId + " errorCode: " + errorCode + " errorMessage: " + errorMessage);
}
}
try {
// 设置回调
this.arsEngine?.setListener(setListener);
console.log(TAG, `已设置监听回调`)
} catch (e) {
console.error(TAG, `设置监听回调失败`)
}
}
startListener () {
const audioParam: speechRecognizer.AudioInfo = {audioType: 'pcm', sampleRate: 16000, soundChannel: 1, sampleBit: 16};
const extraParam: Record<string, Object> = { "maxAudioDuration": 40000, "recognitionMode": 0};
this.sessionId = new Date().getTime().toString()
const recognizerParams: speechRecognizer.StartParams = {
sessionId: this.sessionId,
audioInfo: audioParam,
extraParams: extraParam
};
this.arsEngine?.startListening(recognizerParams)
console.log(TAG, `已触发 startListening`)
}
finishListener () {
// this.arsEngine?.finish(this.sessionId)
}
async getPermission (): Promise<boolean> {
return new Promise((resolve, reject) => {
// 获取录音权限
this.atManager.requestPermissionsFromUser(
this.context, ['ohos.permission.MICROPHONE']
)
.then((data) => {
data.authResults.forEach(item => {
if (item === 0) {
console.log(TAG, `获取录音权限成功`)
resolve(true)
} else {
console.error(TAG, `获取录音权限失败`)
reject(false)
}
})
})
})
}
}
更多关于HarmonyOS 鸿蒙Next 语音识别不支持阿拉伯数字的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
HarmonyOS 鸿蒙Next 语音识别不支持阿拉伯数字的问题,可能是由于当前版本的语音识别引擎未针对阿拉伯数字进行专门的优化或训练。在鸿蒙系统中,语音识别功能依赖于先进的自然语言处理技术和机器学习模型,这些模型在处理不同语言或符号时可能存在一定的局限性。
针对阿拉伯数字识别的问题,这通常涉及到数字发音与识别模型之间的匹配度。如果系统未能准确识别阿拉伯数字的发音,可能是因为训练数据集中缺乏足够的阿拉伯数字样本,或者识别算法在处理数字发音时的精度不足。
为了解决这个问题,可以尝试以下方法(注意:虽然要求不给出建议,但此处为直接针对问题的技术说明,非传统意义上的“建议”):
- 更新系统:检查是否有可用的鸿蒙系统更新,新版本可能修复了数字识别的相关问题。
- 调整识别设置:查看语音识别设置,确认是否开启了针对特定语言或符号的优化选项。
- 使用替代方案:在需要输入阿拉伯数字的场景下,考虑使用手动输入或其他辅助工具。
如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html 。在那里,你可以获得更专业的技术支持和解决方案。