小程序录音 mp3

本文介绍了如何使用WeChat小程序的WXML和JS实现录音、暂停、播放和发送功能。开发者通过`wx.getRecorderManager`获取录音管理器,展示了如何触发录音、停止录音,并利用`createInnerAudioContext`播放录音。

demo.wxml

<view>
  <button bindtap='startRecordMp3' type='primary'>录音开始(mp3)</button>
</view>
<view>
  <button bindtap='stopRecord' type='primary'>录音结束</button>
</view>
<view>
  <button bindtap='playRecord' type='primary'>播放录音</button>
</view>
<view>
  <button bindtap='sendRecord' type='primary'>播放录音</button>
</view>
  1. demo.wxss
view{
  padding: 15px;
}
  1. demo.js
// pages/newMusic/index.js
const recorderManager = wx.getRecorderManager();
Page({

    data: {
        
    },

    /**
    * 提示
    */
    tip: function (msg) {
        wx.showModal({
            title: '提示',
            content: msg,
            showCancel: false
        })
    }

    /**
     * 录制mp3音频
    */
    , startRecordMp3: function () {
        recorderManager.start({
            format: 'mp3'
        });
    }

    /**
     * 停止录音
     */
    , stopRecord: function () {
        recorderManager.stop()
    }

    /**
     * 播放录音
     */
    , playRecord: function () {
        var that = this;
        var src = this.data.src;
        if (src == '') {
            this.tip("请先录音!")
            return;
        }
        this.innerAudioContext.src = this.data.src;
        this.innerAudioContext.play()
    },

    onLoad: function (options) {
        var that = this;
        recorderManager.onError(function () {
            that.tip("录音失败!")
        });
        recorderManager.onStop(function (res) {
            that.setData({
                src: res.tempFilePath
            })
            console.log(res.tempFilePath)
            that.tip("录音完成!")
        });

        this.innerAudioContext = wx.createInnerAudioContext();
        this.innerAudioContext.onError((res) => {
            that.tip("播放录音失败!")
        })

    }

})

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值