更新记录
1.0.2(2025-08-23)
完善示例文档
1.0.1(2025-08-23)
添加示例文档
1.0.0(2025-08-23)
1.0.0
查看更多平台兼容性
云端兼容性
阿里云 | 腾讯云 | 支付宝云 |
---|---|---|
√ | √ | √ |
uni-app(4.75)
Vue2 | Vue3 | Chrome | Safari | app-vue | app-nvue | Android | iOS | 鸿蒙 |
---|---|---|---|---|---|---|---|---|
- | - | - | - | - | - | - | - | - |
微信小程序 | 支付宝小程序 | 抖音小程序 | 百度小程序 | 快手小程序 | 京东小程序 | 鸿蒙元服务 | QQ小程序 | 飞书小程序 | 快应用-华为 | 快应用-联盟 |
---|---|---|---|---|---|---|---|---|---|---|
- | - | - | - | - | - | - | - | - | - | - |
uni-app x(4.75)
Chrome | Safari | Android | Android插件版本 | iOS | 鸿蒙 | 微信小程序 |
---|---|---|---|---|---|---|
- | - | 16.0 | 1.0.0 | - | - | - |
xs-recorder
开发文档
UTS 语法 UTS API插件 UTS uni-app兼容模式组件 UTS 标准模式组件 Hello UTS
使用示例
<template>
<view class="container">
<button @click="startRecording">开始录音</button>
<button @click="stopRecording">结束录音</button>
</view>
</template>
<script setup lang="uts">
import { ref } from 'vue'
import { CreateAudioRecorder, AudioRecorder } from '@/uni_modules/xs-recorder'
import type { AudioRecorderOptions, AudioRecorderCallbacks } from '@/uni_modules/xs-recorder'
const isRecording = ref(false);
const arraybuffer = ref<ArrayBuffer | null>(null);
const filePath = ref('');
const audioRecorder = ref<AudioRecorder | null>(null);
const recodConfig = ref<AudioRecorderOptions>({
filePath: ''
})
// 初始化录音器
const initRecorder = () => {
// 生成文件路径
const timestamp = Date.now();
recodConfig.value.filePath = `recording_${timestamp}.pcm`;
audioRecorder.value = CreateAudioRecorder(recodConfig.value);
// 设置回调
audioRecorder.value?.setCallbacks({
onStart: () => {
isRecording.value = true;
arraybuffer.value = null;
console.log('开始录音');
},
onStop: () => {
isRecording.value = false;
console.log('停止录音');
},
onError: (error ?: Any) => {
console.error('录音错误:', error);
},
onFileStream: (arraybuffer) => {
arraybuffer.value = arraybuffer;
}
} as AudioRecorderCallbacks);
};
const startRecording = async () => {
if (audioRecorder.value == null) {
initRecorder();
}
try {
const success = await audioRecorder.value!.startRecording();
if (!success) {
uni.showToast({
title: '开始录音失败',
icon: 'none'
});
}
} catch (error) {
console.error('开始录音失败:', error);
}
};
const stopRecording = async () => {
try {
await audioRecorder.value?.stopRecording();
} catch (error) {
console.error('停止录音失败:', error);
}
};
// 页面加载时
onLoad(() => {
if (audioRecorder.value?.isRecording() != null) {
stopRecording();
}
})
</script>