更新记录
0.0.3(2026-02-26)
0.0.2(2026-02-26)
0.0.1(2026-02-26)
查看更多
平台兼容性
uni-app(4.87)
| Vue2 |
Vue3 |
Chrome |
Safari |
app-vue |
app-nvue |
Android |
iOS |
鸿蒙 |
| - |
- |
- |
- |
- |
- |
5.0 |
- |
- |
| 微信小程序 |
支付宝小程序 |
抖音小程序 |
百度小程序 |
快手小程序 |
京东小程序 |
鸿蒙元服务 |
QQ小程序 |
飞书小程序 |
小红书小程序 |
快应用-华为 |
快应用-联盟 |
| - |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
- |
uni-app x(4.87)
| Chrome |
Safari |
Android |
iOS |
鸿蒙 |
微信小程序 |
| - |
- |
5.0 |
- |
- |
- |
lime-ali-nui
基于阿里云nuisdk的uni-app语音服务插件,支持语音识别(ASR)和文件转写。
功能特性
- 语音识别(ASR):支持实时语音识别,支持VAD(语音活动检测)
- 文件转写(极速版):支持将音频文件转换为文本,支持在线音频文件URL
- 多种认证方式:支持Token、AccessKey、STS Token三种认证方式
- 简洁API:使用success/fail回调处理结果,简化开发流程
- TypeScript支持:完整的类型定义,提供良好的开发体验
平台支持
- ✅ Android (API 21+)
- ❌ iOS
- ❌ HarmonyOS
快速开始
安装插件
- 在uni-app插件市场中搜索并导入
lime-ali-nui
- 导入后可能需要重新编译项目
- 需要在自定义基座中使用
基本使用
1. 语音识别(ASR)
import useAliNuiSpeech from '@/uni_modules/lime-ali-nui'
import type { AliNuiSpeechCallback, AliNuiSpeechStartOptions, UseAliNuiOptions } from '@/uni_modules/lime-ali-nui'
const speech = useAliNuiSpeech({
appKey: 'your-app-key',
token: 'your-token',
success: () => {
console.log('语音识别初始化成功')
},
fail: (error) => {
console.error('语音识别初始化失败', error)
}
} as UseAliNuiOptions)
// 设置回调
speech.setCallback({
onStarted: (taskId) => {
console.log('识别开始', taskId)
},
OfSpeech: (taskId) => {
console.log('开始说话', taskId)
},
onResult: (result) => {
console.log('识别结果', result.text)
},
onCompleted: (result) => {
console.log('识别完成', result.text)
},
onError: (taskId, error) => {
console.error('识别错误', error)
}
} as AliNuiSpeechCallback)
// 开始识别
speech.start({
format: 'pcm', // 音频格式
sampleRate: 16000, // 采样率
enableVad: true, // 启用VAD
success: (result) => {
console.log('识别启动成功', result)
},
fail: (error) => {
console.error('识别启动失败', error)
}
} as AliNuiSpeechStartOptions)
// 停止识别
speech.stop()
// 取消识别
speech.cancel()
// 释放资源
speech.dispose()
2. 文件转写(极速版)
import useAliNuiFileTranscriber from '@/uni_modules/lime-ali-nui'
import type { AliNuiFileTranscriberStartOptions, UseAliNuiOptions } from '@/uni_modules/lime-ali-nui'
const fileTranscriber = useAliNuiFileTranscriber({
appKey: 'your-app-key',
token: 'your-token',
success: () => {
console.log('文件转写初始化成功')
},
fail: (error) => {
console.error('文件转写初始化失败', error)
}
} as UseAliNuiOptions)
// 开始文件转写
fileTranscriber.start({
filePath: 'https://example.com/audio.wav', // 支持本地文件路径或在线音频URL
sampleRate: 16000, // 音频采样率
format: 'wav', // 音频格式
success: (result) => {
console.log('文件转写成功', result.text)
},
fail: (error) => {
console.error('文件转写失败', error)
}
} as AliNuiFileTranscriberStartOptions)
// 取消转写
fileTranscriber.cancel()
// 释放资源
fileTranscriber.dispose()
API 文档
useAliNuiSpeech
创建语音识别实例。
useAliNuiSpeech(options: UseAliNuiOptions): AliNuiSpeech
UseAliNuiOptions
| 参数 |
类型 |
必填 |
说明 |
| appKey |
string |
是 |
项目AppKey |
| token |
string |
否 |
Token |
| accessKey |
string |
否 |
AccessKey ID |
| accessKeySecret |
string |
否 |
AccessKey Secret |
| stsToken |
string |
否 |
STS Token |
| url |
string |
否 |
自定义服务地址 |
| debug |
boolean |
否 |
是否开启调试模式 |
| success |
() => void |
否 |
成功回调 |
| fail |
(error: any) => void |
否 |
失败回调 |
AliNuiSpeech
语音识别实例。
方法
| 方法 |
说明 |
| start(options: AliNuiSpeechStartOptions) |
开始语音识别 |
| stop() |
停止识别 |
| cancel() |
取消识别 |
| setCallback(callback: AliNuiSpeechCallback) |
设置回调 |
| dispose() |
释放资源 |
AliNuiSpeechCallback
| 方法 |
说明 |
| onStarted?(taskId: string) |
识别开始 |
| onBeginOfSpeech?(taskId: string) |
开始说话 |
| onResult?(result: AliNuiSpeechResult) |
识别结果 |
| onCompleted?(result: AliNuiSpeechResult) |
识别完成 |
| onError?(taskId: string, error: any) |
识别错误 |
AliNuiSpeechStartOptions
| 参数 |
类型 |
必填 |
说明 |
| format |
string |
否 |
音频格式,默认'pcm' |
| sampleRate |
number |
否 |
采样率,默认16000 |
| enableVad |
boolean |
否 |
是否启用VAD,默认true |
| enablePunctuation |
boolean |
否 |
是否添加标点,默认false |
| enableIntermediateResult |
boolean |
否 |
是否返回中间结果,默认false |
| success |
(result: AliNuiSpeechResult) => void |
否 |
成功回调 |
| fail |
(error: any) => void |
否 |
失败回调 |
AliNuiSpeechResult
| 属性 |
类型 |
说明 |
| taskId |
string |
任务ID |
| text |
string |
识别文本 |
| isFinal |
boolean |
是否为最终结果 |
| confidence |
number |
置信度 |
| raw |
object |
原始结果 |
useAliNuiFileTranscriber
创建文件转写(极速版)实例。
useAliNuiFileTranscriber(options: UseAliNuiOptions): AliNuiFileTranscriber
UseAliNuiOptions
| 参数 |
类型 |
必填 |
说明 |
| appKey |
string |
是 |
项目AppKey |
| token |
string |
否 |
Token |
| accessKey |
string |
否 |
AccessKey ID |
| accessKeySecret |
string |
否 |
AccessKey Secret |
| stsToken |
string |
否 |
STS Token |
| url |
string |
否 |
自定义服务地址 |
| debug |
boolean |
否 |
是否开启调试模式 |
| success |
() => void |
否 |
成功回调 |
| fail |
(error: any) => void |
否 |
失败回调 |
AliNuiFileTranscriber
文件转写(极速版)实例。
方法
| 方法 |
说明 |
| start(options: AliNuiFileTranscriberStartOptions) |
开始文件转写 |
| cancel() |
取消转写 |
| dispose() |
释放资源 |
AliNuiFileTranscriberStartOptions
| 参数 |
类型 |
必填 |
说明 |
| filePath |
string |
是 |
音频文件路径或在线音频URL |
| format |
string |
否 |
音频格式,默认根据文件扩展名判断 |
| sampleRate |
number |
否 |
音频采样率,默认16000 |
| enableInverseTextNormalization |
boolean |
否 |
是否启用逆文本标准化 |
| enableWordLevelResult |
boolean |
否 |
是否返回词级结果 |
| firstChannelOnly |
boolean |
否 |
是否只处理第一个声道 |
| success |
(result: AliNuiFileTranscriberResult) => void |
否 |
成功回调 |
| fail |
(error: any) => void |
否 |
失败回调 |
AliNuiFileTranscriberResult
| 属性 |
类型 |
说明 |
| taskId |
string |
任务ID |
| text |
string |
转写文本 |
| isFinal |
boolean |
是否为最终结果 |
| confidence |
number |
置信度 |
| raw |
object |
原始结果 |
错误码
| 错误码 |
说明 |
| 9200001 |
初始化失败 |
| 9200002 |
参数错误 |
| 9200003 |
认证失败 |
| 9200004 |
网络错误 |
| 9200006 |
语音识别失败 |
| 9200007 |
录音权限被拒绝 |
| 9200008 |
未初始化 |
| 9200009 |
SDK文件缺失 |
| 9200010 |
录音设备错误 |
| 9200012 |
Token过期 |
| 9200013 |
音频文件读取失败 |
权限说明
插件需要以下权限:
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
注意事项
- 认证方式:推荐使用Token认证方式,避免在客户端存储AccessKey和AccessKeySecret
- 权限申请:首次使用需要申请录音权限
- 资源释放:使用完毕后务必调用dispose()释放资源
- 网络环境:需要稳定的网络连接
- SDK版本:基于阿里云nuisdk v2.7.2开发
获取AppKey和Token
- 登录阿里云控制台
- 创建项目,获取AppKey
- 根据认证方式获取相应的凭证:
- Token:通过服务端生成Token
- AccessKey:在阿里云控制台右上角获取
- STS Token:通过STS服务获取临时凭证
相关文档
许可证
MIT License
贡献
欢迎提交Issue和Pull Request。
更新日志
1.1.0 (2026-02-26)
- 添加文件转写功能
- 支持本地文件和在线音频URL
- 优化转写结果解析逻辑
- 简化API接口,移除setCallback方法
- 使用success/fail回调处理转写结果
1.0.0 (2024-02-25)
- 初始版本发布
- 支持语音识别(ASR)
- 支持多种认证方式
- 完整的TypeScript类型定义