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

HarmonyOS
2024-12-25 16:45:04
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
aquaa

请参考如下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%')

      // 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`)
  }

  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)
            }
          })
        })
    })
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.
  • 139.
  • 140.
  • 141.
  • 142.
  • 143.
  • 144.
  • 145.
  • 146.
  • 147.
  • 148.
  • 149.
  • 150.
  • 151.
  • 152.
  • 153.
  • 154.
  • 155.
  • 156.
  • 157.
  • 158.
  • 159.
  • 160.
  • 161.
  • 162.
  • 163.
  • 164.
  • 165.
  • 166.
  • 167.
  • 168.
  • 169.
  • 170.
  • 171.
  • 172.
  • 173.
  • 174.
  • 175.
  • 176.
  • 177.
  • 178.
  • 179.
  • 180.
  • 181.
  • 182.
  • 183.
  • 184.
  • 185.
  • 186.
  • 187.
  • 188.
  • 189.
  • 190.
  • 191.
  • 192.
  • 193.
  • 194.
  • 195.
  • 196.
  • 197.
  • 198.
  • 199.
  • 200.
  • 201.
  • 202.
  • 203.
  • 204.
  • 205.
  • 206.
  • 207.
  • 208.
  • 209.
  • 210.
  • 211.
  • 212.
  • 213.
  • 214.
  • 215.
  • 216.
  • 217.
  • 218.
  • 219.
  • 220.
  • 221.
  • 222.
  • 223.
  • 224.
  • 225.
分享
微博
QQ
微信
回复
2024-12-25 19:32:25
相关问题
ArkTS语言支持语音识别吗?
1956浏览 • 1回复 待解决
HarmonyOS 编码集不支持
755浏览 • 1回复 待解决
HarmonyOS 推送设备不支持
795浏览 • 1回复 待解决
Toggle isOn不支持$$?
782浏览 • 1回复 待解决
web组件不支持localstorage
1488浏览 • 1回复 待解决
HarmonyOS Span不支持n换行
732浏览 • 1回复 待解决
HarmonyOS image不支持mask吗
796浏览 • 1回复 待解决
HarmonyOS @State不支持HashMap吗
725浏览 • 1回复 待解决
HarmonyOS 语音识别报错
885浏览 • 1回复 待解决
HarmonyOS 语音识别SDK
774浏览 • 1回复 待解决
HarmonyOS ArkTD不支持any,如何替换
712浏览 • 1回复 待解决
HarmonyOS color文件不支持rgba吗
764浏览 • 1回复 待解决
HarmonyOS RN使用datetimePicker显示不支持
744浏览 • 1回复 待解决
http类不支持cancel方法
866浏览 • 1回复 待解决
HarmonyOS 不支持通过索引访问字段
1117浏览 • 1回复 待解决
HarmonyOS RN不支持相册路径上传
601浏览 • 1回复 待解决
HarmonyOS RNOH Image组件不支持apng
668浏览 • 1回复 待解决
HarmonyOS ets不支持匿名类吗?
960浏览 • 2回复 待解决
HarmonyOS Object不支持 ... 展开符吗?
1245浏览 • 1回复 待解决