HarmonyOS 鸿蒙Next 语音识别不支持阿拉伯数字

发布于 1周前 作者 phonegap100 来自 鸿蒙OS

HarmonyOS 鸿蒙Next 语音识别不支持阿拉伯数字

采用实时录音识别,不支持识别阿拉伯数字?这不合理吧

2 回复
深色代码主题
复制
伙伴您好,
请参考如下Demo,是可以识别数字的。
深色代码主题
复制
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%’)

  <span class="hljs-comment">// voice底部弹窗</span>
  <span class="hljs-title class_">Panel</span>(<span class="hljs-variable language_">this</span>.<span class="hljs-property">show</span>) {
    <span class="hljs-title class_">Column</span>({ <span class="hljs-attr">space</span>: <span class="hljs-number">15</span> }) {
      <span class="hljs-title class_">Text</span>(<span class="hljs-variable language_">this</span>.<span class="hljs-property">voiceText</span>)
        .<span class="hljs-title function_">height</span>(<span class="hljs-number">60</span>)
        .<span class="hljs-title function_">padding</span>({ <span class="hljs-attr">left</span>: <span class="hljs-number">20</span>, <span class="hljs-attr">right</span>: <span class="hljs-number">20</span> })
        .<span class="hljs-title function_">alignSelf</span>(<span class="hljs-title class_">ItemAlign</span>.<span class="hljs-property">Stretch</span>)
      <span class="hljs-title class_">Column</span>() {
        <span class="hljs-title class_">Text</span>(<span class="hljs-string">'按住说话'</span>)
          .<span class="hljs-title function_">fontSize</span>(<span class="hljs-number">14</span>)
          .<span class="hljs-title function_">fontColor</span>(<span class="hljs-string">'#999'</span>)
      }
      .<span class="hljs-title function_">height</span>(<span class="hljs-number">30</span>)
      <span class="hljs-title class_">Stack</span>() {
        <span class="hljs-title class_">Image</span>($rawfile(<span class="hljs-string">'icon_microphone.png'</span>))
          .<span class="hljs-title function_">width</span>(<span class="hljs-number">25</span>)
      }
      .<span class="hljs-title function_">size</span>({ <span class="hljs-attr">width</span>: <span class="hljs-number">50</span>, <span class="hljs-attr">height</span>: <span class="hljs-number">50</span> })
      .<span class="hljs-title function_">backgroundColor</span>(<span class="hljs-string">'#a309ff22'</span>)
      .<span class="hljs-title function_">borderRadius</span>(<span class="hljs-number">100</span>)
      .<span class="hljs-title function_">outline</span>({ <span class="hljs-attr">width</span>: <span class="hljs-number">0</span> })
      .<span class="hljs-title function_">stateStyles</span>({
        <span class="hljs-attr">pressed</span>: <span class="hljs-variable language_">this</span>.<span class="hljs-property">pressedStyles</span>
      })
      .<span class="hljs-title function_">onTouch</span>(<span class="hljs-keyword">async</span> (event) =&gt; {
        <span class="hljs-keyword">if</span> (event.<span class="hljs-property">type</span> === <span class="hljs-title class_">TouchType</span>.<span class="hljs-property">Down</span>) {
          <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">startListener</span>()
        } <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (event.<span class="hljs-property">type</span> === <span class="hljs-title class_">TouchType</span>.<span class="hljs-property">Up</span>) {
          <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">finishListener</span>()
        }
      })
    }
    .<span class="hljs-title function_">width</span>(<span class="hljs-string">'100%'</span>)
    .<span class="hljs-title function_">height</span>(<span class="hljs-string">'100%'</span>)
    .<span class="hljs-title function_">justifyContent</span>(<span class="hljs-title class_">FlexAlign</span>.<span class="hljs-property">Center</span>)
  }
  .<span class="hljs-title function_">width</span>(<span class="hljs-string">'100%'</span>)
  .<span class="hljs-title function_">type</span>(<span class="hljs-title class_">PanelType</span>.<span class="hljs-property">Foldable</span>)
  .<span class="hljs-title function_">mode</span>(<span class="hljs-title class_">PanelMode</span>.<span class="hljs-property">Half</span>)
  .<span class="hljs-title function_">customHeight</span>(<span class="hljs-title class_">PanelHeight</span>.<span class="hljs-property">WRAP_CONTENT</span>)
  .<span class="hljs-title function_">dragBar</span>(<span class="hljs-literal">true</span>) <span class="hljs-comment">// 默认开启</span>
  .<span class="hljs-title function_">halfHeight</span>(<span class="hljs-number">300</span>) <span class="hljs-comment">// 默认一半</span>
  .<span class="hljs-title function_">showCloseIcon</span>(<span class="hljs-literal">true</span>) <span class="hljs-comment">// 显示关闭图标</span>
  .<span class="hljs-title function_">position</span>({ <span class="hljs-attr">x</span>: <span class="hljs-number">0</span> })
  .<span class="hljs-title function_">backgroundColor</span>(<span class="hljs-title class_">Color</span>.<span class="hljs-property">White</span>)
  .<span class="hljs-title function_">onChange</span>(<span class="hljs-function">(<span class="hljs-params">width: number, height: number, mode: PanelMode</span>) =&gt;</span> {
    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-variable constant_">TAG</span>, <span class="hljs-string">`width: <span class="hljs-subst">${width}</span>, height: <span class="hljs-subst">${height}</span>, mode: <span class="hljs-subst">${mode}</span>`</span>)
  })
  .<span class="hljs-title function_">onHeightChange</span>(<span class="hljs-function">(<span class="hljs-params">height: number</span>) =&gt;</span> {
    <span class="hljs-keyword">if</span> (height &lt;= <span class="hljs-number">0</span>) {
      <span class="hljs-variable language_">this</span>.<span class="hljs-property">voiceLastText</span> = <span class="hljs-variable language_">this</span>.<span class="hljs-property">voiceText</span>
    }
  })
}
.<span class="hljs-title function_">height</span>(<span class="hljs-string">'100%'</span>)
.<span class="hljs-title function_">backgroundColor</span>(<span class="hljs-string">'#ffe7e7e7'</span>)

}

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, 获取语音转文字实例失败 <span class="hljs-subst">${e.code}</span> <span class="hljs-subst">${e.message}</span>) } }

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 。在那里,你可以获得更专业的技术支持和解决方案。

回到顶部