更新记录

1.0.0(2026-09-27)

语音识别


平台兼容性

uni-app(5.26)

Vue2 Vue3 Chrome Safari app-vue app-nvue Android iOS 鸿蒙
√ √ × × √ √ × 12 16
微信小程序 支付宝小程序 抖音小程序 百度小程序 快手小程序 京东小程序 鸿蒙元服务 QQ小程序 飞书小程序 小红书小程序 快应用-华为 快应用-联盟
× × × × × × × × × × × ×

uni-app x(5.26)

Chrome Safari Android iOS 鸿蒙 微信小程序
× × × 12 16 ×

yao-speechrecog

UTS API 插件:基于系统原生 API 的语音识别,无需接入任何第三方 SDK 和密钥。

平台 实现方式 状态
iOS Speech framework(SFSpeechRecognizer) 已支持
HarmonyOS Core Speech Kit(@kit.CoreSpeechKit,离线识别) 已支持
Android 系统无统一可用识别引擎(国产 ROM 普遍缺失),暂不支持 未支持

引入

import * as speechrecog from '@/uni_modules/yao-speechrecog'

API

speechrecog.isSupported(): boolean

当前设备/平台是否支持语音识别。

speechrecog.start(options, listener)

开始语音识别,内部会自动申请麦克风 / 语音识别权限。

options: StartOptions

属性 类型 说明
lang string 可选。识别语言,如 "zh-CN";不传默认 "zh-CN"(鸿蒙当前主要支持中文普通话,iOS 可传 "en-US" 等)
partialResults boolean 可选。是否返回中间(部分)识别结果,默认 true

listener: (event: SpeechRecogEvent) => void

SpeechRecogEvent:

属性 类型 说明
type string 事件类型,见下表
text string 识别文本(partialResult / finalResult 时有效)
errCode number 错误码(error 时有效)
errMsg string 错误描述(error 时有效)

事件类型 type:

值 说明
start 开始监听
readyForSpeech 已就绪,可以开始说话
speechStart 检测到用户开始说话(仅 iOS 触发)
speechEnd 检测到说话结束(仅 iOS 触发)
partialResult 中间识别结果
finalResult 最终识别结果
error 出错(见 errCode / errMsg)
end 本次识别会话结束(成功、失败、取消均会触发)

错误码:

码 说明
10001 麦克风 / 语音识别权限被拒绝
10002 设备不支持 / 识别引擎创建失败
10003 音频错误(录音启动失败)
10004 网络 / 识别服务错误
10005 没有检测到语音输入
10006 没有匹配到识别结果
10007 识别服务繁忙
10008 其他错误

speechrecog.stop()

停止录音,等待引擎返回最终结果(触发 finalResult -> end)。

speechrecog.cancel()

取消识别,丢弃结果(只触发 end)。

Vue3 示例

<template>
  <view class="page">
    <button :type="recording ? 'warn' : 'primary'" @click="toggle">
      {{ recording ? '停止识别' : '开始识别' }}
    </button>
    <text class="result">{{ result || '识别结果将显示在这里' }}</text>
  </view>
</template>

<script setup>
import { ref, onUnmounted } from 'vue'
import * as speechrecog from '@/uni_modules/yao-speechrecog'

const recording = ref(false)
const result = ref('')

function startRecog() {
  if (!speechrecog.isSupported()) {
    uni.showToast({ title: '当前设备不支持语音识别', icon: 'none' })
    return
  }
  result.value = ''
  speechrecog.start({ lang: 'zh-CN', partialResults: true }, (ev) => {
    switch (ev.type) {
      case 'readyForSpeech':
        recording.value = true
        break
      case 'partialResult': // 中间结果,实时上屏
        result.value = ev.text
        break
      case 'finalResult': // 最终结果
        result.value = ev.text
        break
      case 'error':
        uni.showToast({ title: `识别出错(${ev.errCode}):${ev.errMsg}`, icon: 'none' })
        break
      case 'end': // 无论成功、失败、取消都会触发
        recording.value = false
        break
    }
  })
}

function stopRecog() {
  // 停止录音并等待引擎返回最终结果(触发 finalResult -> end)
  speechrecog.stop()
}

function toggle() {
  recording.value ? stopRecog() : startRecog()
}

// 页面卸载时释放资源
onUnmounted(() => {
  speechrecog.cancel()
})
</script>

<style>
.page { padding: 40rpx; }
.result { margin-top: 40rpx; color: #333; }
</style>

Vue2 示例

<template>
  <view class="page">
    <button :type="recording ? 'warn' : 'primary'" @click="toggle">
      {{ recording ? '停止识别' : '开始识别' }}
    </button>
    <text class="result">{{ result || '识别结果将显示在这里' }}</text>
  </view>
</template>

<script>
import * as speechrecog from '@/uni_modules/yao-speechrecog'

export default {
  data() {
    return {
      recording: false,
      result: ''
    }
  },
  methods: {
    startRecog() {
      if (!speechrecog.isSupported()) {
        uni.showToast({ title: '当前设备不支持语音识别', icon: 'none' })
        return
      }
      this.result = ''
      // 注意用箭头函数,保证回调内 this 指向组件
      speechrecog.start({ lang: 'zh-CN', partialResults: true }, (ev) => {
        switch (ev.type) {
          case 'readyForSpeech':
            this.recording = true
            break
          case 'partialResult':
            this.result = ev.text
            break
          case 'finalResult':
            this.result = ev.text
            break
          case 'error':
            uni.showToast({ title: `识别出错(${ev.errCode}):${ev.errMsg}`, icon: 'none' })
            break
          case 'end':
            this.recording = false
            break
        }
      })
    },
    stopRecog() {
      speechrecog.stop()
    },
    toggle() {
      this.recording ? this.stopRecog() : this.startRecog()
    }
  },
  // 页面卸载时释放资源
  onUnload() {
    speechrecog.cancel()
  }
}
</script>

<style>
.page { padding: 40rpx; }
.result { margin-top: 40rpx; color: #333; }
</style>

HarmonyOS

  • 基于 Core Speech Kit 离线识别引擎(online: 1),当前主要支持中文普通话(zh-CN)。
  • 需要在鸿蒙工程中声明麦克风权限:运行到鸿蒙后,在生成的鸿蒙工程 entry/src/main/module.json5 的 module 节点内加入:
"requestPermissions": [
  { "name": "ohos.permission.INTERNET" },
  {
    "name": "ohos.permission.MICROPHONE",
    "reason": "$string:permission_microphone_reason",
    "usedScene": {}
  }
]

并在 entry/src/main/resources/base/element/string.json(建议连同 zh_CN)补充:

{ "name": "permission_microphone_reason", "value": "用于语音识别功能" }
  • 引擎单次会话上限 60 秒,插件检测到会话到期会自动累计文本并重开会话,实现连续识别。

隐私、权限声明

1. 本插件需要申请的系统权限列表:

需要麦克风权限

2. 本插件采集的数据、发送的服务器地址、以及数据用途说明:

插件不采集任何数据

3. 本插件是否包含广告,如包含需详细说明广告表达方式、展示频率:

无

暂无用户评论。