更新记录
1.0.0(2025-11-24)
初版功能
平台兼容性
uni-app(3.6.14)
| Vue2 |
Vue2插件版本 |
Vue3 |
Vue2插件版本 |
Chrome |
Safari |
app-vue |
app-vue插件版本 |
app-nvue |
app-nvue插件版本 |
Android |
Android插件版本 |
iOS |
鸿蒙 |
| √ |
1.0.0 |
√ |
1.0.0 |
- |
- |
√ |
1.0.0 |
√ |
1.0.0 |
5.0 |
1.0.0 |
× |
× |
| 微信小程序 |
支付宝小程序 |
抖音小程序 |
百度小程序 |
快手小程序 |
京东小程序 |
鸿蒙元服务 |
QQ小程序 |
飞书小程序 |
快应用-华为 |
快应用-联盟 |
| × |
× |
× |
× |
× |
× |
× |
× |
× |
× |
× |
uni-app x(3.91)
| Chrome |
Safari |
Android |
iOS |
鸿蒙 |
微信小程序 |
| - |
- |
- |
- |
- |
- |
其他
yk-asr
uts实现安卓原生语音识别功能
方法
| 方法名 |
说明 |
参数 |
| init |
初始化语音识别 |
- |
| start |
开始识别 |
StartOptions |
| stop |
停止识别 |
- |
| destroyAsr |
销毁识别器实例 |
- |
StartOptions 参数说明
StartOptions = {
locale ?: string, // 语言区域,默认为 zh-CN
onStart ?: (status: string) => void, // 开始识别回调
onPartial ?: (text: string) => void, // 实时识别中间结果回调
onDone ?: (text: string) => void, // 最终识别结果回调
onError ?: (msg: string, code: number) => void // 错误回调
}
需要权限
"<uses-permission android:name=\"android.permission.RECORD_AUDIO\"/>"
// 有些设备可能需要网络权限
"<uses-permission android:name=\"android.permission.INTERNET\"/>"
使用示例
<template>
<view class="container">
<view class="title">语音识别 Demo</view>
<button
type="primary"
@click="toggleListening"
class="btn">
{{ listening ? '正在听…点我停止' : '按住说话' }}
</button>
<view class="result">
<text class="label">实时文字:</text>
<text class="content">{{ partialText || '无' }}</text>
</view>
<view class="result">
<text class="label">最终结果:</text>
<text class="content">{{ finalText || '无' }}</text>
</view>
</view>
</template>
<script setup>
// 正确路径(uni_modules 方式)
import { start, stop, destroyAsr } from '@/uni_modules/yk-asr'
import { ref, onUnmounted } from 'vue'
// 状态
const listening = ref(false)
const partialText = ref('')
const finalText = ref('')
// ==================== 必须:页面卸载时销毁,防止“回调已释放” ====================
onUnmounted(() => {
destroyAsr()
console.log('页面已卸载,yk-asr 已销毁')
})
// ==================== 请求录音权限(必须) ====================
function requestAudioPermission() {
return new Promise((resolve) => {
plus.android.requestPermissions(
['android.permission.RECORD_AUDIO'],
(res) => {
// res.granted 为 true 表示用户点了“允许”
resolve(!!res.granted)
},
() => resolve(false)
)
})
}
async function toggleListening() {
if (listening.value) {
stop()
listening.value = false
return
}
const ok = await requestAudioPermission()
if (!ok) return uni.showToast({ title: '需要麦克风权限', icon: 'none' })
partialText.value = ''
finalText.value = ''
start({
locale: 'zh-CN',
// 每次 start 都传入最新的回调 → 永远不会被释放!
onStart: (status) => {
console.log('状态', status)
listening.value = true
},
onPartial: (text) => {
partialText.value = text
},
onDone: (text) => {
finalText.value = text
partialText.value = text
listening.value = false
uni.showToast({ title: '识别完成', icon: 'success' })
},
onError: (msg, code) => {
listening.value = false
uni.showModal({ title: '错误 ' + code, content: msg, showCancel: false })
}
})
}
</script>
<style scoped>
.container { padding: 40rpx; }
.title { font-size: 48rpx; font-weight: bold; text-align: center; margin: 60rpx 0; color: #333; }
.btn { margin: 60rpx 0; height: 100rpx; line-height: 100rpx; font-size: 36rpx; }
.result { margin-top: 50rpx; background: #f9f9f9; padding: 30rpx; border-radius: 20rpx; }
.label { font-size: 32rpx; color: #666; }
.content { font-size: 40rpx; color: #333; margin-top: 20rpx; font-weight: bold; }
</style>