vcloud-fe-utils
v1.0.0
Published
A collection of useful browser utilities and tools
Maintainers
Readme
vcloud-fe-utils
一个通用的浏览器工具库集合,提供各种实用的工具函数和类。
功能特性
- 🎯 TypeScript支持 - 完整的类型定义
- 📦 零依赖 - 不依赖任何第三方库
- 🚀 轻量级 - 体积小,性能优秀
- 🔧 模块化 - 按需导入,支持Tree Shaking
安装
npm install vcloud-fe-utils
# 或
yarn add vcloud-fe-utils
# 或
pnpm add vcloud-fe-utils功能模块
🎤 音频录制 (WavRecorder)
高质量音频录制和WAV格式转换工具。
基本使用
import { WavRecorder } from 'vcloud-fe-utils'
// 创建录音器实例
const recorder = new WavRecorder()
// 开始录音
try {
await recorder.startRecording()
console.log('录音已开始')
} catch (error) {
console.error('无法访问麦克风:', error)
}
// 停止录音并获取Base64格式的WAV音频
try {
const base64Audio = await recorder.stopRecording()
console.log('录音完成,音频数据长度:', base64Audio.length)
// 保存为本地文件
WavRecorder.saveBase64AsWavFile(base64Audio, 'my-recording.wav')
} catch (error) {
console.error('录音处理失败:', error)
}完整示例
import { WavRecorder } from 'vcloud-fe-utils'
class AudioRecorder {
private recorder: WavRecorder
private isRecording: boolean = false
constructor() {
this.recorder = new WavRecorder()
}
// 开始录音
async start() {
if (this.isRecording) return
try {
await this.recorder.startRecording()
this.isRecording = true
console.log('🎤 录音开始')
} catch (error) {
console.error('❌ 无法开始录音:', error)
}
}
// 停止录音
async stop(): Promise<string | null> {
if (!this.isRecording) return null
try {
const base64 = await this.recorder.stopRecording()
this.isRecording = false
console.log('✅ 录音完成')
return base64
} catch (error) {
console.error('❌ 录音停止失败:', error)
return null
}
}
// 保存录音文件
saveRecording(base64: string, filename: string = 'recording.wav') {
WavRecorder.saveBase64AsWavFile(base64, filename)
console.log(`💾 文件已保存: ${filename}`)
}
}
// 使用示例
const audioRecorder = new AudioRecorder()
// 开始录音
document.getElementById('startBtn')?.addEventListener('click', () => {
audioRecorder.start()
})
// 停止并保存录音
document.getElementById('stopBtn')?.addEventListener('click', async () => {
const audioData = await audioRecorder.stop()
if (audioData) {
audioRecorder.saveRecording(audioData, 'my-audio.wav')
}
})API 文档
WavRecorder 类
实例方法:
startRecording(): Promise<void>- 开始录音stopRecording(): Promise<string>- 停止录音并返回Base64编码的WAV音频数据getRecordingState(): boolean- 获取当前录音状态getChunksCount(): number- 获取已录制的数据块数量
静态方法:
saveBase64AsWavFile(base64: string, fileName?: string): void- 将Base64字符串保存为本地WAV文件base64ToBlob(base64: string, mimeType?: string): Blob- 将Base64字符串转换为Blob对象
技术细节
- 音频格式: 自动选择浏览器支持的最佳格式(WebM/MP4/OGG)
- 采样率: 16kHz
- 声道: 单声道
- 位深: 16bit
- 文件格式: 标准WAV格式(PCM编码)
更多功能
更多实用工具正在开发中,敬请期待!
浏览器支持
支持所有现代浏览器:
- Chrome 50+
- Firefox 50+
- Safari 11+
- Edge 79+
开发
# 克隆仓库
git clone <repository-url>
# 安装依赖
pnpm install
# 构建
pnpm build
# 开发模式(监听文件变化)
pnpm dev
# 测试
pnpm test
# 代码检查
pnpm lint贡献
欢迎提交Issue和Pull Request!
许可证
MIT License
更新日志
v1.0.0
- 初始版本发布
- 添加音频录制功能(WavRecorder)
- 支持WAV格式转换
- 提供文件保存功能
- 完整的TypeScript类型定义
