微信小程序 录音实现上传 和播放录音

// pages/recorderManager/recorderManager.js
const recorderManager = wx.getRecorderManager();
const innerAudioContext = wx.createInnerAudioContext();
Page({
  data: {
    openRecordingdis: "block",//录音图片的不同
    shutRecordingdis: "none",//录音图片的不同
    recordingTimeqwe:0,//录音计时
    setInter:"",//录音名称
    isplay:true //播放状态  true--播放中  false--暂停播放
  },
 
   //录音计时器
  recordingTimer:function(){
    var that = this;
    //将计时器赋值给setInter
    that.data.setInter = setInterval(
      function () {
        var time = that.data.recordingTimeqwe + 1;
        if(time>60){
          wx.showToast({
            title: '录音时长到一分钟啦',
            duration:1500,
            mask:true
          })
          clearInterval(that.data.setInter);
          that.shutRecording();
          return;
        }
        that.setData({
          recordingTimeqwe: time
        })
      }
      , 1000); 
  },
 
 
  //开始录音
  openRecording: function() {
    var that = this;
    wx.getSystemInfo({
      success: function(res) {
        that.setData({
          shutRecordingdis: "block",
          openRecordingdis: "none"
        })
      }
    })
    const options = {
      duration: 60000, //指定录音的时长,单位 ms,最大为10分钟(600000),默认为1分钟(60000)
      sampleRate: 16000, //采样率
      numberOfChannels: 1, //录音通道数
      encodeBitRate: 96000, //编码码率
      format: 'mp3', //音频格式,有效值 aac/mp3
      frameSize: 50, //指定帧大小,单位 KB
    }
    //开始录音计时   
    that.recordingTimer();
    //开始录音
    recorderManager.start(options);
    recorderManager.onStart(() => {
      console.log('。。。开始录音。。。')
    });
    //错误回调
    recorderManager.onError((res) => {
      console.log(res);
    })
  },
 
  //结束录音
  shutRecording: function() {
    var that = this;
    wx.getSystemInfo({
      success: function(res) {
        that.setData({
          shutRecordingdis: "none",
          openRecordingdis: "block"
        })
      }
    })
    recorderManager.stop();
    recorderManager.onStop((res) => {
      console.log('。。停止录音。。', res.tempFilePath)
      const {tempFilePath} = res;
      //结束录音计时  
      clearInterval(that.data.setInter);
      //上传录音
      wx.uploadFile({
        url: "http://www.xinjue.com:8080/imageUpload?fileType=mp3",//这是你自己后台的连接
        filePath: tempFilePath,
        name:"file",//后台要绑定的名称
        header: {
          "Content-Type": "multipart/form-data"
        },
        //参数绑定
        formData:{
          recordingtime: that.data.recordingTimeqwe,
          topicid: that.data.topicid,
          userid:1,
          praisepoints:0
        },
        success:function(ress){
          console.log(res);
          wx.showToast({
            title: '保存完成',
            icon:'success',
            duration:2000
          })
          that.setData({
            recordingTimeqwe:0
          })
        },
        fail: function(ress){
          console.log("。。录音保存失败。。");
        }
      })
    })
  },
 
  //录音播放
  recordingAndPlaying: function(e) {
    var isplay = e.currentTarget.dataset.isplay; //暂停还是播放
    innerAudioContext.src ="http://soundpreview.ohipic.com/preview/sound/00/29/33/515ppt-S293364-183B1F2B.mp3";
    innerAudioContext.startTime=0; //播放起始位置
    innerAudioContext.autoplay = true;//是否自动播放
    if (isplay == true){
      isplay = false;
      //播放事件
      innerAudioContext.onPlay(() => {
        console.log('开始播放');
      })
    }else{
      isplay = true;
      //监听暂停
      innerAudioContext.onPause(() => {
        console.log('暂停播放');
      })
    }
   this.setData({
     isplay:isplay
   })
  },
 
})
<!--pages/recorderManager/recorderManager.wxml-->
<text>录音页面编写</text>
<button bindtap="openRecording" size="50rpx">开始录音</button>
<button bindtap="stop">暂停录音</button>
<button bindtap="shutRecording">结束录音</button>
<button bindtap="recordingAndPlaying" data-isplay="{{isplay}}">播放录音</button>
<form bindsubmit="submit" report-submit='true' class='formid'>
      <button form-type="submit" >发送推送消息</button>
      </form>
      <!-- <view class="img-box">
        <canvas bindtap="previewImg" hidden="{{canvasHidden}}" style="width: 686rpx;height: 686rpx;background:#f1f1f1;" canvas-id="mycanvas"/>
        <!--<image bindtap="previewImg" mode="scaleToFill" src="{{imagePath}}"></image>
    </view> -->

使用最新的API  

InnerAudioContext 实例,可通过 wx.createInnerAudioContext 接口获取实例。

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

码奴生来只知道前进~

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

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

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

打赏作者

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

抵扣说明:

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

余额充值