更新记录
1.1.8(2026-03-23)
增加uniappx demo项目
1.1.7(2026-03-23)
增加ios支持,语速设置
1.1.6(2024-08-30)
优化
查看更多平台兼容性
uni-app(4.57)
| Vue2 | Vue3 | Chrome | Safari | app-vue | app-nvue | Android | Android插件版本 | iOS | iOS插件版本 | 鸿蒙 |
|---|---|---|---|---|---|---|---|---|---|---|
| - | - | - | - | - | - | 5.0 | 1.0.0 | 16 | 1.1.7 | - |
| 微信小程序 | 支付宝小程序 | 抖音小程序 | 百度小程序 | 快手小程序 | 京东小程序 | 鸿蒙元服务 | QQ小程序 | 飞书小程序 | 小红书小程序 | 快应用-华为 | 快应用-联盟 |
|---|---|---|---|---|---|---|---|---|---|---|---|
| - | - | - | - | - | - | - | - | - | - | - | - |
uni-app x(4.57)
| Chrome | Safari | Android | Android插件版本 | iOS | iOS插件版本 | 鸿蒙 | 微信小程序 |
|---|---|---|---|---|---|---|---|
| - | - | 5.0 | 1.0.0 | 16 | 1.1.7 | - | - |
xx-audioRecord
录音与文字转语音 UTS 插件。
当前插件提供两类能力:
- 录音:开始录音、停止录音,输出
pcm和wav文件 - TTS:初始化语音服务、朗读文本、生成语音文件、读取支持语言列表
平台支持
- Android:支持录音、TTS 朗读、TTS 文件生成
- iOS:支持录音、TTS 朗读、TTS 文件生成
- Web / 小程序 / Harmony:当前未实现
开发文档
安装后注意事项
- Android 首次录音会触发麦克风权限申请
- iOS 需要在
manifest.json中配置麦克风权限描述NSMicrophoneUsageDescription - 插件内部默认把文件写到应用缓存目录;如果传入
path,会写入指定目录 - 页面播放录音文件时,uni-app x 建议使用
uni.createInnerAudioContext(),不要依赖audio组件
导出接口
import {
startAudioRecord,
stopAudioRecord,
useTextToSpeech
} from "@/uni_modules/xx-audioRecord"
录音 API
startAudioRecord(options, callback)
开始录音,并准备输出 pcm 和 wav 文件。
参数说明:
path?: string | null文件输出目录。为空时使用应用缓存目录。sampleRateInHz?: number | null采样率。默认44100。channelConfig?: number | null声道配置。传1表示单声道,其它值按双声道处理。audioFormat?: number | null位深配置。传8表示PCM 8bit,其它值按PCM 16bit处理。
回调结果:
code: 1开始录制成功code < 0开始录制失败
返回字段:
code: numbermessage: stringpcmFilePath?: stringwavFilePath?: string
示例:
import { startAudioRecord } from "@/uni_modules/xx-audioRecord"
startAudioRecord({
path: null,
sampleRateInHz: 16000,
channelConfig: 1,
audioFormat: 16
}, (result) => {
console.log("startAudioRecord", result)
})
stopAudioRecord(callback)
停止录音,并返回最终生成的文件路径。
回调结果:
code: 1录制完成code < 0停止失败
示例:
import { stopAudioRecord } from "@/uni_modules/xx-audioRecord"
stopAudioRecord((result) => {
console.log("stopAudioRecord", result)
console.log("pcm:", result.pcmFilePath)
console.log("wav:", result.wavFilePath)
})
TTS API
new useTextToSpeech(options)
创建 TTS 实例。
参数说明:
pitch?: number音调,默认1speechRate?: number语速,默认1languageTag?: string语言标签,例如zh-CN、en-USsuccess?: (result) => void初始化成功回调fail?: (result) => void初始化失败回调complete?: (result) => void初始化完成回调
初始化回调结果:
code: 0初始化成功code: -1初始化失败code: -2语言不支持
示例:
import { useTextToSpeech } from "@/uni_modules/xx-audioRecord"
const tts = new useTextToSpeech({
pitch: 1,
speechRate: 1,
languageTag: "zh-CN",
success: (result) => {
console.log("tts init success", result)
},
fail: (result) => {
console.log("tts init fail", result)
},
complete: (result) => {
console.log("tts init complete", result)
}
})
tts.speak(options)
朗读文本。
参数说明:
text?: string | nullonStart?: (result) => voidonDone?: (result) => voidonError?: (result) => void
示例:
tts.speak({
text: "这是一个文字转语音测试",
onStart: (result) => {
console.log("speak onStart", result)
},
onDone: (result) => {
console.log("speak onDone", result)
},
onError: (result) => {
console.log("speak onError", result)
}
})
tts.synthesizeToFile(options)
生成语音文件。
参数说明:
text?: string | nullfileName?: string | null仅指定目标文件名,不同平台实际编码格式由系统实现决定onStart?: (result) => voidonDone?: (result) => voidonError?: (result) => void
返回字段:
code: numberfilePath?: stringmessage?: string
示例:
tts.synthesizeToFile({
text: "将这段文本输出成语音文件",
fileName: "tts-demo.wav",
onStart: (result) => {
console.log("synthesize onStart", result)
},
onDone: (result) => {
console.log("synthesize onDone", result)
console.log("filePath", result.filePath)
},
onError: (result) => {
console.log("synthesize onError", result)
}
})
tts.getAvailableLanguages()
获取当前设备支持的语言列表。
返回字段:
displayName: stringdisplayNameChina: stringlanguageTag: string
示例:
const languages = tts.getAvailableLanguages()
console.log("languages", languages)
tts.setPitch(value)
设置音调。
tts.setPitch(1.2)
tts.setSpeechRate(value)
设置语速。
tts.setSpeechRate(1.1)
tts.stop()
停止当前朗读或文件生成任务。
tts.stop()
tts.destroy()
销毁 TTS 实例,不再使用时应主动调用。
tts.destroy()
完整示例
import {
startAudioRecord,
stopAudioRecord,
useTextToSpeech
} from "@/uni_modules/xx-audioRecord"
let tts: useTextToSpeech | null = null
function initTts() {
if (tts != null) {
tts.destroy()
tts = null
}
tts = new useTextToSpeech({
pitch: 1,
speechRate: 1,
languageTag: "zh-CN",
success: (result) => {
console.log("init success", result)
},
fail: (result) => {
console.log("init fail", result)
}
})
}
function startRecord() {
startAudioRecord({
path: null,
sampleRateInHz: 16000,
channelConfig: 1,
audioFormat: 16
}, (result) => {
console.log("record start", result)
})
}
function stopRecord() {
stopAudioRecord((result) => {
console.log("record stop", result)
})
}
function speakText() {
if (tts == null) return
tts.speak({
text: "你好,这是插件示例",
onDone: (result) => {
console.log("speak done", result)
}
})
}
function synthesizeFile() {
if (tts == null) return
tts.synthesizeToFile({
text: "你好,这是文件生成示例",
fileName: "demo.wav",
onDone: (result) => {
console.log("file done", result)
}
})
}
注意事项
startAudioRecord录音中不要重复调用,否则会返回失败stopAudioRecord只能在录音已开始后调用speak和synthesizeToFile前,建议先完成 TTS 初始化languageTag建议使用标准 IETF 格式,例如zh-CN、en-US- iOS 的 TTS 文件生成依赖系统能力,低版本系统可能不支持
- 生成的录音和 TTS 文件都建议按业务需要及时清理,避免缓存目录长期堆积
Demo
仓库中的 pages/index/index.uvue 提供了完整演示,包括:
- 录音生成
pcm/wav - 使用
uni.createInnerAudioContext()回放录音文件 - TTS 初始化、朗读、生成文件
- 读取语言列表与日志输出

收藏人数:
购买源码授权版(
试用
使用 HBuilderX 导入示例项目
赞赏(0)
下载 72
赞赏 0
下载 11526262
赞赏 1902
赞赏
京公网安备:11010802035340号