小程序調(diào)用百度TTS語音合成接口js代碼


var msg, token, IMEI, filePath

Page({

  /** 

  * 頁面的初始數(shù)據(jù) 

  */

  data: {

  },

  /** 

  * 生命周期函數(shù)--監(jiān)聽頁面加載 

  */

  onLoad: function (options) {

    var that=this

    wx.getSystemInfo({

      success: function (res) {

        console.log(res)

        IMEI = res.SDKVersion

        console.log(IMEI)

      }

    }) ,

    //建立連接

    wx.connectSocket({

      url: "ws://123.207.167.163:9010/ajaxchattest",

      success:function(res){

        console.log(res)

      }

    })

    var socketOpen = false

    var socketMsgQueue = []

    //連接成功監(jiān)聽WebSocket連接打開事件。

    wx.onSocketOpen(function () {

      console.log("-----連接成功")

      socketOpen = true

    that.sendMessage(msg)

    })

    //監(jiān)聽WebSocket接受到服務(wù)器的消息事件接收數(shù)據(jù)

    wx.onSocketMessage(function (res) {

      msg=res.data;

      console.log('接收數(shù)據(jù)----', msg);

      var contact = JSON.parse(msg);

    that.setData({

      msg: contact.msg

    })

    })

    //連接失敗

    wx.onSocketError(function () {

      console.log('websocket連接失?。?);

    })

  },

  sendMessage: function (msg) {

    msg="您好,歡迎光臨";

    var data = {

      msg: msg

    }

    wx.sendSocketMessage({

      data: JSON.stringify(data),

      success: function (res) {

        console.log(res)

      }

    })

  },

  tts:function(e){

      var grant_type ="client_credentials";

      var appKey ="7A6fuVBxIgjCCMu0eMhsxyuu";

      var appSecret ="HMxdXoT0FCGWSkgiCZQclcoHBswYyP0p";

    //  var url = "https://openapi.baidu.com/oauth/2.0/token" + "grant_type=" + grant_type + "&client_id=" + appKey + "&client_secret=" + appSecret

      var url = "https://openapi.baidu.com/oauth/2.0/token"

    wx.request({

      url: url,

      data: {

        grant_type: grant_type,

        client_id: appKey,

        client_secret: appSecret

      },

      method: "GET",

      header: {

        'content-type': 'application/json' // 默認(rèn)值

      },

      success: function (res) {

        console.log(res.data)

        token = res.data.access_token

      }

    })

  },

    // 合成

    cancel: function (e) {

      var text = JSON.parse(msg).msg;

      var tex = encodeURI(text);//轉(zhuǎn)換編碼url_encode UTF8編碼

      var tok = token;

      var cuid = IMEI;

      var ctp=1;

      var lan = "zh";    // zh表示中文

    // 字符編碼

      var spd = 5;  // 表示朗讀的語速,9代表最快,1是最慢(撩妹請用2,繞口令請用9)

      var url = "https://tsn.baidu.com/text2audio?tex=" + tex  + "&lan=" + lan + "&cuid=" + cuid + "&ctp=" + ctp + "&tok=" + tok + "&spd=" +spd

      wx.downloadFile({

        url: url,

        success: function (res) {

          console.log(res)

          filePath=res.tempFilePath;

          // 只要服務(wù)器有響應(yīng)數(shù)據(jù),就會把響應(yīng)內(nèi)容寫入文件并進(jìn)入 success 回調(diào),業(yè)務(wù)需要自行判斷是否下載到了想要的內(nèi)容

          if (res.statusCode === 200) {

            wx.playVoice({

              filePath: res.tempFilePath

            })

          }

        }

      })

  },

  //播放

    play: function (e) {

      const innerAudioContext = wx.createInnerAudioContext()

      innerAudioContext.autoplay = true

      innerAudioContext.src = filePath

      innerAudioContext.onPlay(() => {

        console.log('開始播放')

      })

      innerAudioContext.onError((res) => {

        console.log(res.errMsg)

        console.log(res.errCode)

      })

    },

})

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容