小程序录音播放

今天做一个简陋的录音效果,下面就直接上代码

<view bindtouchstart="handleTouchStart" bindtouchend="handleTouchEnd" bindlongpress="handleLongPress" bindtap="handleClick">开始录音</view>
//touch start
      handleTouchStart:  function (e)  {
                this.startTime  =  e.timeStamp;
                //console.log(" startTime = " + e.timeStamp);  
      },

      //touch end
      handleTouchEnd:  function (e)  {
                this.endTime  =  e.timeStamp;
            wx.stopRecord() // 结束录音
                //console.log(" endTime = " + e.timeStamp);  
      },

      handleClick:  function (e)  {
                //console.log("endTime - startTime = " + (this.endTime - this.startTime));    
                if  (this.endTime  -  this.startTime  <  350)  {
                      console.log("点击");
                      this.playVoice()
                }
      },

      handleLongPress:  function (e)  {
            //console.log("endTime - startTime = " + (this.endTime - this.startTime));
            console.log("长按");
            this.record()
      },

      record: function() {
            let that = this;
            wx.startRecord({
                  success(res) {
                        const tempFilePath = res.tempFilePath
                        console.log(tempFilePath)
                        that.setData({
                              tempFilePath: tempFilePath
                        })
                  }
            })
      },

      playVoice: function() {
            let that = this;
            let tempFilePath = that.data.tempFilePath
            wx.playVoice({
                  filePath: tempFilePath,
                  complete() { }
            })
      },
发布了40 篇原创文章 · 获赞 0 · 访问量 991

猜你喜欢

转载自blog.csdn.net/HXH_csdn/article/details/103609196