微信小程序录音、播放源码【推荐】

【-----------------------文章尾部附加源码下载地址-----------------------】

1.前端wxml代码,很简单的一个按钮、一些说明文字、一个语音循环列表:

<!--index.wxml-->
<view style="margin-top: 50rpx;width: 100%;display: flex;justify-content: center;color: red;">
  点击录音/松开停止
</view>

<button type="primary" style="margin-top: 50rpx;" catchtouchstart="start" catchtouchend="end">
  录音
</button>

<view wx:if="{{records.length>0}}" style="margin-top: 50rpx;width: 100%;display: flex;justify-content: center;color: red;">
  点击下方录音文件播放
</view>

<view wx:for="{{records}}" wx:key="item" data-item="{{item}}" bindtap="clickPlay" style="width: 100%;margin-top: 10rpx;background: #E3e3e3; height: 100rpx; display: flex;justify-content: center;align-items: center;">
  <view style="width: 90%;display:-webkit-box;overflow:hidden;text-overflow:ellipsis;-webkit-line-clamp:1;">
    {{item}}
  </view>
</view>

界面:
在这里插入图片描述

2.js功能代码块。主要包含三个功能:开始录音、停止录音、播放声音:

  start() {
    var that=this
    const options = {
      duration: that.data.duration,
      sampleRate: 16000,
      numberOfChannels: 1,
      encodeBitRate: 24000,
      format: 'mp3',
      frameSize: 50
    }
    recorderManager.start(options)
    wx.showToast({
      title: '录音中...',
      mask: false,
      duration: that.data.duration,
      icon: "none",
    })
  },

  end() {
    recorderManager.stop()
  },

  clickPlay(e){
    var that=this
    console.log(e.currentTarget.dataset.item)
    const innerAudioContext = wx.createInnerAudioContext()
    innerAudioContext.autoplay = true
    innerAudioContext.src = e.currentTarget.dataset.item
    innerAudioContext.onPlay(() => {
      wx.showToast({
        title: '播放中...',
        mask: true,
        duration: that.data.duration,
        icon: "none",
      })
    })

    innerAudioContext.onEnded(()=>
    {
      console.log('播放结束')
      wx.hideToast({
        success: (res) => {},
      })
    })

    innerAudioContext.onError((res) => {
      console.log(res.errMsg)
      console.log(res.errCode)
    })
  },

上面只是简单的核心代码,下载录音源码Demo地址:

链接:https://pan.baidu.com/s/1wJ_MqNyhA_s9QTj0hV0nBw
提取码:nucj

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

猴狲

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值