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

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

浏览量:723 时间: 来源:码奴生来只知道前进~
// pages/recorderManager/recorderManager.jsconst 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(time60){          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   })  }, })

使用最新的API  

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

版权声明

即速应用倡导尊重与保护知识产权。如发现本站文章存在版权问题,烦请提供版权疑问、身份证明、版权证明、联系方式等发邮件至197452366@qq.com ,我们将及时处理。本站文章仅作分享交流用途,作者观点不等同于即速应用观点。用户与作者的任何交易与本站无关,请知悉。

产品经理

手机 : 13312967497

擅长 : 小程序流量变现

扫码领取礼包

热门模板

  • 头条
  • 搜狐
  • 微博
  • 百家
  • 一点资讯
  • 知乎