溫馨提示×

溫馨提示×

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

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

微信小程序語音識別、語音合成代碼怎么寫

發(fā)布時間:2022-04-20 15:40:45 來源:億速云 閱讀:520 作者:iii 欄目:大數(shù)據(jù)

今天小編給大家分享一下微信小程序語音識別、語音合成代碼怎么寫的相關知識點,內(nèi)容詳細,邏輯清晰,相信大部分人都還太了解這方面的知識,所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來了解一下吧。

使用步驟:

1、 在微信公眾平臺配置,找到設置–第三方設置–插件管理–點擊增加插件,

搜索微信同聲傳譯并增加

2、 在項目根目錄app.json文件中配置

"plugins": {

    "WechatSI": {

      "version": "0.3.4",

      "provider": "wx069ba97219f66d99"

    }

  },

語音合成:

在pages的js中加入插件初始化代碼

const innerAudioContext = wx.createInnerAudioContext();

innerAudioContext.autoplay = true;

const plugin = requirePlugin('WechatSI');

因為語音合成原理是微信同聲傳譯是在同聲傳譯后端生產(chǎn)錄音,下載播放錄音??梢栽陧撁婕虞d階段生產(chǎn)錄音,在使用的地方播放錄音,就不會有推遲。

//在全局定義變量

var remoteAudio = null;

//在開始階段加載

plugin.textToSpeech({

     lang: "en_US",

     tts: true,

     content: word,

     success: function(res) {

         console.log("succ tts", res.filename) 

        // this.playAudio(res.filename);

        remoteAudio = res.filename;

     },

     fail: function(res) {

         console.log("fail tts", res)

     }

})

  },

//在實際需要使用語音合成地方

innerAudioContext.stop();

console.log("remoteAudio: " + remoteAudio);

innerAudioContext.src = remoteAudio;

innerAudioContext.play();

innerAudioContext.onError((e) => {

  console.log(e.errMsg)     

  console.log(e.errCode)     

  })

語音識別:

在pages的js中加入插件初始化代碼

//引入插件:微信同聲傳譯

const plugin = requirePlugin('WechatSI');

//獲取全局唯一的語音識別管理器recordRecoManager

const manager = plugin.getRecordRecognitionManager();

// 設置采集聲音參數(shù)

const options = {

  sampleRate: 44100,

  numberOfChannels: 1,

  encodeBitRate: 192000,

  format: 'aac'

}

在 onload()中加入初始化代碼

//識別語音

this.initRecord();

在需要加入語音識別地方加入下面代碼:

//語音  --按住說話

  touchStart: function(e) {

    wx.vibrateShort() //按鍵震動效果(15ms)

    manager.start(options)

    this.setData({

      recordState: true, //錄音狀態(tài)為真

      tips: '松開結束',

    })

  },

  //語音  --松開結束

  touchEnd: function(e) {

    // 語音結束識別

    manager.stop();

    this.setData({

      recordState: false,

    })

  },

  //識別語音 -- 初始化

  initRecord: function() {

    const that = this;

    // 有新的識別內(nèi)容返回,則會調(diào)用此事件

    manager.onRecognize = function(res) {

      console.log(res)

    }

    // 正常開始錄音識別時會調(diào)用此事件

    manager.onStart = function(res) {

      console.log("成功開始錄音識別", res)

    }

    // 識別錯誤事件

    manager.onError = function(res) {

      console.error("error msg:", res.retcode, res.msg)

    }

    //識別結束事件

    manager.onStop = function(res) {

      console.log('..............結束錄音')

      console.log('錄音總時長 -->' + res.duration + 'ms');

      console.log('語音內(nèi)容 --> ' + res.result);

      if (res.result == '') {

        wx.showModal({

          title: '提醒',

          content: '聽不清楚,請重新說一遍!',

          showCancel: false,

          success: function(res) {}

        })

        return;

      }

      //下面有些代碼有少量業(yè)務代碼,要根據(jù)自己實際進行替換

      if(res.result == this.myword){

        that.setData({

          content: that.myword + '讀音正確' //去掉自動增加的句號

        })

          next();

      }else{

        that.setData({

          recordState: false, //錄音狀態(tài)為真

          content: that.myword +'讀音不準',

        })

        plugin.textToSpeech({

          lang: "en_US",

          tts: true,

          content: that.myword,

          success: function(res) {

              console.log("succ tts", res.filename)               

          },

          fail: function(res) {

              console.log("fail tts", res)

          }

      })

      }

    }

  },

以上就是“微信小程序語音識別、語音合成代碼怎么寫”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會為大家更新不同的知識,如果還想學習更多的知識,請關注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

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

AI