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

HarmonyOS
1天前
浏览
收藏 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)
            }
          })
        })
    })
  }
}
分享
微博
QQ
微信
回复
1天前
相关问题
ArkTS语言支持语音识别吗?
1111浏览 • 1回复 待解决
web组件不支持localstorage
831浏览 • 1回复 待解决
HarmonyOS 语音识别报错
51浏览 • 1回复 待解决
HarmonyOS 语音识别SDK
42浏览 • 1回复 待解决
HarmonyOS Span不支持n换行
20浏览 • 1回复 待解决
HarmonyOS image不支持mask吗
36浏览 • 1回复 待解决
HarmonyOS RN使用datetimePicker显示不支持
103浏览 • 1回复 待解决
http类不支持cancel方法
201浏览 • 1回复 待解决
HarmonyOS 不支持通过索引访问字段
331浏览 • 1回复 待解决
HarmonyOS ets不支持匿名类吗?
241浏览 • 2回复 待解决
HarmonyOS Object不支持 ... 展开符吗?
288浏览 • 1回复 待解决
HarmonyOS Text的Span不支持align
17浏览 • 1回复 待解决
HarmonyOS imagePacker不支持编码成jpg格式
451浏览 • 1回复 待解决