更新记录
1.2.4(2026-03-08)
1.2.3(2026-02-26)
1.2.2(2026-02-14)
查看更多
平台兼容性
uni-app(4.87)
| Vue2 |
Vue3 |
Chrome |
Safari |
app-vue |
app-nvue |
Android |
Android插件版本 |
iOS |
iOS插件版本 |
鸿蒙 |
鸿蒙插件版本 |
| - |
- |
- |
- |
- |
- |
5.0 |
1.0.0 |
13 |
1.0.9 |
19 |
1.0.4 |
| 微信小程序 |
支付宝小程序 |
抖音小程序 |
百度小程序 |
快手小程序 |
京东小程序 |
鸿蒙元服务 |
QQ小程序 |
飞书小程序 |
小红书小程序 |
快应用-华为 |
快应用-联盟 |
| - |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
uni-app x(4.87)
| Chrome |
Safari |
Android |
Android插件版本 |
iOS |
iOS插件版本 |
鸿蒙 |
鸿蒙插件版本 |
微信小程序 |
| - |
- |
5.0 |
1.0.0 |
13 |
1.0.9 |
19 |
1.0.4 |
- |
xwq-sherpa-onnx
开发文档说明
插件功能 (兼容安卓、鸿蒙端、iOS)
- 实时语音识别
- 离线语音识别(wav音频文件转文本识别)
中英双语模型可以下载:sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20
鸿蒙端使用特别说明
* 鸿蒙端使用sherpa_onnx语音识别特别说明:
* 将路径:unpackage\dist\dev\app-harmony\entry\build-profile.json5文件拷贝到根目录harmony-configs/entry下(目录没有的需要手动创建)
*
* 在harmony-configs/entry/build-profile.json5中配置参数字段:buildOption下面新增以下内容
* "sourceOption": {
* "workers": [
* '../uni_modules/xwq-sherpa-onnx/utssdk/app-harmony/onnxWorkers.ets'
* ]
* }
模型资源需要放在插件目录resources/rawfile/onnxModel下面才能正常使用
| 属性 |
类型 |
默认值 |
必填 |
描述 |
| model |
string |
- |
N |
模型路径,单模型的情况使用 |
| encoder |
string |
- |
N |
编码器模型路径,多模型的情况使用,例如:transducer模型 |
| decoder |
string |
- |
N |
解码器模型路径,多模型的情况使用,例如:transducer模型 |
| joiner |
string |
- |
N |
joiner模型路径,多模型的情况使用,例如:transducer模型 |
| tokens |
string |
- |
N |
tokens文件路径,一般在多模型文件下才会存在,例如:transducer模型 |
| isCopyFile |
boolean |
- |
N |
模型文件的拷贝,默认不拷贝,由页面传入可访问的地址 |
| success |
()=>void |
- |
Y |
初始化模型成功回调 |
| fail |
()=>void |
- |
Y |
初始化模型失败回调 |
| numThreads |
number |
1 |
N |
执行线程数 |
| errCode |
msg |
| -1 |
缺少模型文件地址 |
| -2 |
实例已存在,请勿重复初始化 |
| -3 |
模型文件不存在 |
| -4 |
ASR 初始化失败 |
实时语音识别使用步骤
uesSherpaOnnx({
mode: 'online',
modelType: "transducer",
model: '',
tokens: staticPath + "tokens.txt",
encoder: staticPath + "encoder-epoch-99-avg-1.int8.onnx",
decoder: staticPath + "decoder-epoch-99-avg-1.onnx",
joiner: staticPath + "joiner-epoch-99-avg-1.int8.onnx",
success: () => {
console.log('初始化成功')
uni.hideLoading()
},
fail: (res) => {
console.log(res)
}
})
setListner() {
setRecognizerResultListner((res, final) => {
console.log('识别结果====', res)
console.log('final====', final)
this.content = res
})
}
start() {
startRecognizer()
}
离线语音识别(识别wav音频文件)使用步骤
initOfflineOnnx() {
uni.showLoading({
title: '模型加载中...',
})
let path = '/static/offlineOnnxModel/';
const staticPath = plus.io.convertLocalFileSystemURL(path);
console.log('staticPath===', staticPath)
uesSherpaOnnx({
mode: 'offline',
modelType: "zipformer2ctc",
model: '',
tokens: staticPath + "tokens.txt",
encoder: staticPath + "encoder-epoch-34-avg-19.int8.onnx",
decoder: staticPath + "decoder-epoch-34-avg-19.onnx",
joiner: staticPath + "joiner-epoch-34-avg-19.int8.onnx",
success: () => {
console.log('初始化成功')
uni.hideLoading()
},
fail: (res) => {
console.log(res)
}
})
}
- 2.开始识别wav音频文件(需要单声道、16kHz、16位 PCM 格式)
recognizerFile() {
let path = '/static/offlineOnnxModel/';
const filePath = plus.io.convertLocalFileSystemURL(path) + '1.wav';
startOfflineRecognizerFile(filePath, (result) => {
console.log('识别结果===', result)
this.content = result
})
}
stopRecognizer() //停止实时语音识别
stopOfflineRecognizerFile() //停止音频文件识别
destroyRecognizer() //页面销毁时调用此方法销毁实例,释放内存占用
uniappX页面完整示例
<template>
<view>
<button @click="initOnnx">初始化Onnx</button>
<button @click="setListner">设置结果监听</button>
<button @click="removeRecognizerResultListner">移除结果监听</button>
<button @click="start">开始识别</button>
<button @click="stop">停止识别</button>
<button @click="destroy">销毁实例</button>
<button @click="initOfflineOnnx">初始化离线语音识别器</button>
<button @click="recognizerFile">开始识别WAV File音频文件</button>
<button @click="stopRecognizerFile">停止识别WAV File音频文件</button>
<view style="padding:10px;">
<view class="title">
<text>识别结果:</text>
</view>
<view class="content">
<textarea :value="content" disabled style="padding:15px;background-color: #f2f2f2;width: 100%;"></textarea>
<!-- <text v-for="(i,k) in resultArr" :key="k"></text> -->
</view>
</view>
</view>
</template>
<script setup>
import {
startRecognizer,
uesSherpaOnnx,
stopRecognizer,
setRecognizerResultListner,
removeOnRecognizerResultListner,
startOfflineRecognizerFile,
stopOfflineRecognizerFile,
destroyRecognizer
} from "@/uni_modules/xwq-sherpa-onnx";
import { UseSherpaOnnxOptions } from "@/uni_modules/xwq-sherpa-onnx/utssdk/interface.uts";
const content=ref('');
// 在线识别
const initOnnx=()=> {
uni.showLoading({
title: '模型加载中...',
})
uesSherpaOnnx({
mode: 'online',
modelType: "transducer",
model: '',
tokens: "/static/onlineOnnxModel/tokens.txt",
encoder: "/static/onlineOnnxModel/encoder-epoch-99-avg-1.int8.onnx",
decoder: "/static/onlineOnnxModel/decoder-epoch-99-avg-1.onnx",
joiner: "/static/onlineOnnxModel/joiner-epoch-99-avg-1.int8.onnx",
success: () => {
console.log('初始化成功')
uni.hideLoading()
},
fail: (res) => {
console.log(res)
}
} as UseSherpaOnnxOptions)
};
//离线识别
const initOfflineOnnx=()=> {
uni.showLoading({
title: '模型加载中...',
})
uesSherpaOnnx({
mode: 'offline',
modelType: "zipformer2ctc",
model: '',
tokens: "/static/offlineOnnxModel/tokens.txt",
encoder: "/static/offlineOnnxModel/encoder-epoch-34-avg-19.int8.onnx",
decoder: "/static/offlineOnnxModel/decoder-epoch-34-avg-19.onnx",
joiner: "/static/offlineOnnxModel/joiner-epoch-34-avg-19.int8.onnx",
success: () => {
console.log('初始化成功')
uni.hideLoading()
},
fail: (res) => {
console.log(res)
}
} as UseSherpaOnnxOptions)
};
//开始识别
const start=()=> {
startRecognizer()
};
//停止识别
const stop=()=> {
stopRecognizer()
};
//在不使用时或页面销毁时需调用此方法销毁实例
const destroy=()=>{
destroyRecognizer()
};
//设置监听
const setListner=()=> {
setRecognizerResultListner((res:string, final:boolean) => {
console.log('识别结果====', res)
console.log('final====', final)
content.value = res
})
};
//移除监听
const removeRecognizerResultListner=()=> {
removeOnRecognizerResultListner()
};
//离线识别file(wav格式音频)
const recognizerFile=()=> {
const filePath = '/static/offlineOnnxModel/1.wav';
startOfflineRecognizerFile(filePath, (result) => {
console.log('识别结果===', result)
content.value = result
})
};
//停止离线识别wav file
const stopRecognizerFile=()=> {
stopOfflineRecognizerFile()
};
</script>
<style>
</style>
uniapp页面完整示例例
<template>
<view>
<button @click="initOnnx">初始化Onnx</button>
<button @click="setListner">设置结果监听</button>
<button @click="removeRecognizerResultListner">移除结果监听</button>
<button @click="start">开始识别</button>
<button @click="stop">停止识别</button>
<button @click="destroy">销毁实例</button>
<button @click="initOfflineOnnx">初始化离线语音识别器</button>
<button @click="recognizerFile">开始识别WAV File音频文件</button>
<button @click="stopRecognizerFile">停止识别WAV File音频文件</button>
<view style="padding:10px;">
<view class="title">
<text>识别结果:</text>
</view>
<view class="content">
<textarea :value="content" disabled style="padding:15px;"></textarea>
<!-- <text v-for="(i,k) in resultArr" :key="k"></text> -->
</view>
</view>
</view>
</template>
<script>
import {
startRecognizer,
uesSherpaOnnx,
stopRecognizer,
setRecognizerResultListner,
removeOnRecognizerResultListner,
startOfflineRecognizerFile,
stopOfflineRecognizerFile,
destroyRecognizer
} from "@/uni_modules/xwq-sherpa-onnx";
export default {
data() {
return {
resultArr: [],
content: ""
}
},
mounted() {},
methods: {
// 在线识别
initOnnx() {
uni.showLoading({
title: '模型加载中...',
})
let path = '/static/onlineOnnxModel/';
const staticPath = plus.io.convertLocalFileSystemURL(path);
console.log('staticPath===', staticPath)
uesSherpaOnnx({
mode: 'online',
modelType: "transducer",
model: '',
tokens: staticPath + "tokens.txt",
encoder: staticPath + "encoder-epoch-99-avg-1.int8.onnx",
decoder: staticPath + "decoder-epoch-99-avg-1.onnx",
joiner: staticPath + "joiner-epoch-99-avg-1.int8.onnx",
success: () => {
console.log('初始化成功11')
uni.hideLoading()
},
fail: (res) => {
console.log(res)
}
})
},
//离线识别
initOfflineOnnx() {
uni.showLoading({
title: '模型加载中...',
})
let path = '/static/offlineOnnxModel/';
const staticPath = plus.io.convertLocalFileSystemURL(path);
console.log('staticPath===', staticPath)
uesSherpaOnnx({
mode: 'offline',
modelType: "zipformer2ctc",
model: '',
tokens: staticPath + "tokens.txt",
encoder: staticPath + "encoder-epoch-34-avg-19.int8.onnx",
decoder: staticPath + "decoder-epoch-34-avg-19.onnx",
joiner: staticPath + "joiner-epoch-34-avg-19.int8.onnx",
success: () => {
console.log('初始化成功11')
uni.hideLoading()
},
fail: (res) => {
console.log(res)
}
})
},
//开始识别
start() {
startRecognizer()
},
//停止识别
stop() {
stopRecognizer()
},
//在不使用时或页面销毁时需调用此方法销毁实例
destroy(){
destroyRecognizer()
},
//设置监听
setListner() {
setRecognizerResultListner((res, final) => {
console.log('识别结果====', res)
console.log('final====', final)
this.content = res
})
},
//移除监听
removeRecognizerResultListner() {
removeOnRecognizerResultListner()
},
//离线识别file(wav格式音频)
recognizerFile() {
let path = '/static/offlineOnnxModel/';
const filePath = plus.io.convertLocalFileSystemURL(path) + '1.wav';
startOfflineRecognizerFile(filePath, (result) => {
console.log('识别结果===', result)
this.content = result
})
},
//停止离线识别wav file
stopRecognizerFile() {
stopOfflineRecognizerFile()
}
}
}
</script>
<style>
.title {
margin: 20px 0 10px 0;
}
.content {
height: 100%;
border: 1px solid #ccc;
background-color: #f2f2f2;
}
</style>
其他插件预览