溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

小程序聊天室功能怎么實現(xiàn)

發(fā)布時間:2022-04-16 13:52:24 來源:億速云 閱讀:122 作者:iii 欄目:編程語言

這篇“小程序聊天室功能怎么實現(xiàn)”文章的知識點大部分人都不太理解,所以小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細,步驟清晰,具有一定的借鑒價值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“小程序聊天室功能怎么實現(xiàn)”文章吧。

首先在頭部定義 ...


1.index.js 使用的是微信自帶的api,首先在頭部定義一個

const recorderManager = wx.getRecorderManager()//獲取全局唯一的錄音管理器const createInnerAudioContext = wx.createInnerAudioContext()//由于使用的是wx.getRecorderManager錄音,所以在播放錄音的時候需要使用此api播放錄音
  // 需要用戶同意授權(quán)錄音 方法
  _checkRecordAuth(cbOk, cbError) {
    wx.getSetting({
      success: (res) => {        if (!res.authSetting['scope.record']) {
          wx.authorize({
            scope: 'scope.record',
            success: (res) => {              // 用戶已經(jīng)同意小程序使用錄音功能,后續(xù)調(diào)用 wx.startRecord 接口不會彈窗詢問
              console.log('同意', res);
            }, fail: res => {              console.log('拒絕', res);
              cbError && cbError();
            }
          })
        } else {
          cbOk && cbOk();
        }
      }
    })
  },  // 長按
  _longClickVoicebtn(e) {    var that = this;
      that._checkRecordAuth(        () => {        //調(diào)出取消彈窗
        that.setData({
          text: '松開 發(fā)送',
          isShowVoice1: true,
          singleVoiceTimeCount: 0,
          duration: "00:00",
          is_clock: true,//長按時應(yīng)設(shè)置為true,為可發(fā)送狀態(tài)
          startPoint: e.touches[0],//記錄觸摸點的坐標信息
        });        //開始錄音
        recorderManager.start(options);
        recorderManager.onStart(()=>{          let date = new Date();          let s = date.getTime();//注意:使用的是當(dāng)前的時間戳 - 時長
          console.log(s);          //記錄時長
          that.data.timer = setInterval(() => {            let voiceTimeCount = (new Date()).getTime() - s
            voiceTimeCount = voiceTimeCount/1000
            that.setData({
              duration: formatSecond(voiceTimeCount),
              singleVoiceTimeCount: voiceTimeCount
            })
          }, 100);
        })

       

      },       (res) => {        //錄音失敗
        console.error('錄音拒絕授權(quán)');
        clearInterval(this.data.timer);        this._endRecord();

        wx.showModal({
          title: '您未授權(quán)語音功能',
          content: '暫時不能使用語音',
          confirmText: '去設(shè)置',
          success: res => {            if (res.confirm) {
              wx.openSetting({
                success: res => {                  if (res.authSetting['scope.record']) {

                  }
                }
              });
            } else {

            }
          }
        });
      });
  },// 移動 如果移動到tabar上面了,就顯示 松開手指,取消發(fā)送
  _sendVoiceMoveEvent(e) {      //計算距離,當(dāng)滑動的垂直距離大于 tabBarHeight 時,則取消發(fā)送語音
      if (Math.abs(e.touches[0].clientY - this.data.startPoint.clientY) > this.data.tabBarHeight) {        this.setData({
          is_clock: false,//設(shè)置為不發(fā)送語音
          isShowVoice2: true,
          isShowVoice1: false,
          cance: 1,  //已取消
        })
      } else {        this.setData({
          is_clock: true,//設(shè)置為不發(fā)送語音
          isShowVoice2: false,
          isShowVoice1: true,
          cance: 0,  //不取消
        });
    }
  },  // 松開div 錄音結(jié)束
  _sendVoiceMoveEndEvent(e) {    console.log('松開div 錄音結(jié)束')    var that = this
    clearInterval(this.data.timer);

      that.setData({
        text: '按住 說話',
        isShowVoice1: false,
        isShowVoice2: false,
        singleVoiceTimeCount: 0,
        duration: "00:00"
      });
      recorderManager.stop()//結(jié)束錄音

      //對停止錄音進行監(jiān)控
      recorderManager.onStop((res) => {        
        //此時先判斷是否需要發(fā)送錄音
        if (that.data.is_clock == true) {          console.log('發(fā)送語音')          //對錄音時長進行判斷,少于2s的不進行發(fā)送,并做出提示
          if (res.duration < 1000) {
            that.showToast("錄音時間太短,請長按錄音");
          } else {            //進行語音發(fā)送
            const { tempFilePath, duration, fileSize } = res;
            wx.showLoading({
              title: '上傳中...',
              mask: true
            })
            uploadFile(tempFilePath).then(res => {              console.log(res);              this._sendMessage({
                commentContent: res,
                orgId: that.data.orgId,
                resId: that.data.childId,
                commentContentType: "audio",
                fileSize: fileSize,
                fileTime: parseInt(duration/1000)
              })
            })
          }
        }
      })
  },

2.wxml中,給view添加上

<view catch:longpress="_longClickVoicebtn" catch:touchmove="_sendVoiceMoveEvent" catch:touchend="_sendVoiceMoveEndEvent"hover-class="btn-voice-press">

3、index.js 播放錄音

createInnerAudioContext.autoplay = truecreateInnerAudioContext.src = src //src就是傳的播放地址

以上就是關(guān)于“小程序聊天室功能怎么實現(xiàn)”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望小編分享的內(nèi)容對大家有幫助,若想了解更多相關(guān)的知識內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI