微信小程序录音、停止录音、上传录音、播放录音

页面效果图:

未录音的数据,点击开始录音,点击停止录音上传录音

已录音状态可以播放录音、重新录制功能

代码:
创建内部 audio 上下文 InnerAudioContext 对象
获取全局唯一的录音管理器 RecorderManager

复制代码
//  监听页面加载时,创建内部 audio 上下文 InnerAudioContext 对象
onLoad(options){
	const innerAudioContext = wx.createInnerAudioContext({
		useWebAudioImplement: false // 是否使用 WebAudio 作为底层音频驱动,默认关闭。对于短音频、播放频繁的音频建议开启此选项,开启后将获得更优的性能表现。由于开启此选项后也会带来一定的内存增长,因此对于长音频建议关闭此选项
    })
    // 获取全局唯一的录音管理器 RecorderManager
    const recorderManager = wx.getRecorderManager();
    this.setData({
        innerAudioContext:innerAudioContext,
        recorderManager,
    })
}

点击开始录音

复制代码
handleStart(){
	// 开始录音
    this.data.recorderManager.start({
    	format: 'wav', // 录音格式
    });
    this.setData({
        isAudio:true,
    })
},

点击停止录音

复制代码
handleStop(){
	// 监听录音结束事件
    this.data.recorderManager.onStop((res)=>{
        this.setData({
            isAudio:false,
            audioSrc:res.tempFilePath,  // 录音文件的临时路径
            duration:res.duration/1000  // duration 录音时长,单位ms,我需要的是s,做了处理
        });
        // 停止录音后,上传文件
        this.getUploadFile(res.tempFilePath)
    });
    // 停止录音
    this.data.recorderManager.stop()
},
// 停止录音后,上传文件
getUploadFile(file){
	wx.uploadFile({
		filePath: file,
        name: 'file',
        // 上传文件的接口地址
        url: `${app.globalData.baseUrl}/xxxx/uploadVoice`,
        formData: {
            'user': 'test',
            'file':'blob.ogg'
        },
        header: {
        	Authorization: `Bearer ${wx.getStorageSync('token')}`,
        },
        timeout: 0,
        success: (result) => {
            wx.showToast({
                title: '录音成功',
            })
            // 上传文件成功后,调用接口自动保存录音文件
            this.getUpload(result.data)
        },
        fail: (res) => {
            wx.showToast({
           		title: res.message,
            })
        },
        complete: (res) => {
            console.log(res,'《=====录音成功或失败都会调用');
        },
	})
},

监听录音结束事件返回的res

点击播放录音

复制代码
handlePlay(){
	// 录音文件地址
	this.data.innerAudioContext.src = this.data.item.audioUrl;
	// 播放录音
    this.data.innerAudioContext.play();
    this.setData({
        isPlay:true
    })
    // 监听音频自然播放至结束的事件,播放结束,停止播放
    this.data.innerAudioContext.onEnded(()=>{
        this.handleStopPlay()
    })
},
// 暂停播放
handleStopPlay(){
    this.setData({
        isPlay:false
    });
    this.data.innerAudioContext.stop();
},
相关推荐
拖孩15 小时前
用 AI 重解千年观音灵签,做了一个微信小程序,每天摇一摇,命运给你回应
前端·后端·微信小程序
java1234_小锋20 小时前
【免费】微信小程序网约车(打车,在线叫车,移动出行)系统(SpringBoot4+Vue3) 锋哥原创出品,必属精品
微信小程序·网约车·在线叫车·移动出行
AI砖家2 天前
AI编程—微信小程序开发规范
微信小程序·小程序·notepad++·ai编程
sam-zy3 天前
微信小程序+ESP8266+Arduino 开发智能插座,有本地OTA功能
微信小程序·小程序
A24207349303 天前
微信小程序开发:常用基础语法与指令详解
微信小程序·小程序·notepad++
laboratory agent开发4 天前
2026小程序系统多维度技术与落地能力深度解析
微信小程序
laboratory agent开发6 天前
多维度拆解小程序平台:数字化经营工具的理性选择指南
微信小程序
T01156186 天前
独立开发者商单实战|商超门店小程序(自提 + 同城配送):小程序全端页面 & 业务流程总图汇总
微信小程序·小程序·个人开发·前端实战
驳是7 天前
入坑 UniApp 写小程序,一些感受
微信小程序
不如摸鱼去8 天前
Wot UI 2.3.0 发布:二维码组件来了,Open Wot 与 wot-starter 同步更新
前端·ui·微信小程序·前端框架·uni-app