微信小程序:nodejs+百度语音合成开发实践

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/eadio/article/details/78981853

写在前面,今天突然又整理了这个教程是因为百度的语音合成文本最多可以有1024个字节,而腾讯的只有150个字节。而且开发语言可以自由选择。其中包括nodeJs开发。今天就拿这个做实践。

1、在百度AI开放平台注册账号,并申请应用。申请完可以在应用管理看到如下应用,appid,apikey,secretkey在待会合成的时候会用到。
申请应用

2、安装express框架开发后端项目,教程请去看官网

3、接着使用该框架生成项目,项目结构如下:
后端项目结构

然后这里强调个事:

不要去下载官网提供的node插件,不要去下载官网提供的node插件,不要去下载官网提供的node插件

重要的事情说三遍,这种开发方式具体没研究,我用的是npm安装依赖的方式。

然后进入刚生成的项目:执行npm install baidu-aip-sdk –save-dev安装百度提供的依赖,,如果还不知道npm的,请自定百度。

然后到routes目录下创建新的接口文件AiSpeechSynthesis.js,写入如下代码:

var express=require('express');
var router=express.Router();
var fs=require('fs');
var AipSpeechServer = require('baidu-aip-sdk').speech;

//设置appid/appkey/appsecret
var APP_ID = "1申请的appid";
var API_KEY = "1申请的appkey";
var SECRET_KEY = "1申请的secretkey";

// 新建一个对象,建议只保存一个对象调用服务接口
var client =new AipSpeechServer(APP_ID, API_KEY, SECRET_KEY);

// 语音合成
router.post('/speech', function(req, res, next){
  //console.log('./public/audio/');
  console.log(req.body);//用这种content-type=www-form-urlencoded才能获取到参数
  console.log(req.body.text.length);
  //return ;
  client.text2audio(
    req.body.text || '你好,百度语音合成测试',
    {
      //cuid: '机器 MAC 地址或 IMEI 码,长度为60以内',
      spd: req.body.spd || '5',//音速
      pit: req.body.pit || '5',//音调
      vol: req.body.vol || '5',//音量
      per: req.body.per || '0'//播音角色
    }
  )
  .then(
    function(res1){
      if(res1.data){
        //console.log(res1);
        fs.writeFileSync('./public/audio/tts.audio.mp3', res1.data);

        res.json({
          ret: 0,
          data:{
            path: 'http://你的ip地址:3000/audio/tts.audio.mp3'//返回小程序调用播放
          },
          msg: ''
        });

      }else{
        // 服务发生错误
        console.log(res1);
        res.json({
          ret: res1.err_no,
          data:{
          },
          msg: res1.err_msg
        });
      }
    }, 
    function(e){
      // 发生网络错误
      console.log(e);
      res.json({
        ret: -100,
        data:{
        },
        msg: '网络错误,请检查网络'
      });
    }
  );
});

module.exports=router;

然后找到根目录的app.js做如下配置:

var synthesis=require('./routes/AiSpeechSynthesis.js');
app.use('/baiduAI', synthesis);

然后开启cmd,定位到项目根目录,执行npm start运行项目

4、小程序发起请求得到合成的音频地址:

Page({
  data: {

  },
  onLoad: function (options) {
    let that=this;
   that.innerAudioContext=wx.createInnerAudioContext();
    that.innerAudioContext.autoplay=true;
    that.innerAudioContext.loop=true;
    wx.request({
      url: 'http://你的ip地址:3000/baiduAI/speech',
      data: {
        text: '日光岩,是鼓浪屿每天第一缕阳光照到的地方,俗称""岩仔山"",别名""晃岩"",相传1641年,郑成功来到晃岩,看到这里的景色胜过日本的日光山,便把""晃""字拆开,称之为""日光岩""。日光岩游览区由日光岩和琴园两个部分组成。日光岩耸峙于鼓浪屿中部偏南,是由两块巨石一竖一横相倚而立,成为龙头山的顶峰,海拔92.7米...',
        spd: '6',
        pit: '3',
        vol: '8',
        per: '0'
      },
      method: 'POST',
      success(res) {  
        let data = res.data;
        if (data.ret === 0) {
          that.innerAudioContext.src = data.data.path+'?rnd='+new Date().getTime();
          that.innerAudioContext.onPlay(() => {
            console.log('开始播放啦');
          });
          that.innerAudioContext.onError((res) => {
            console.log(res.errMsg)
            console.log(res.errCode)
          });
        }else{
          wx.showToast({
            title: data.msg,
          });
        }
      },
      fail(err) {
        console.log('err');
        console.log(err)
      }
    });
  }
})

最后演示如下:
演示效果

目前只做到能播放的程度,很多细节也没仔细考虑好,也没有那么多时间。目前正在研究语音识别,涉及到音频转码,比较麻烦。如果有了解大神们,希望告知下,感谢感谢。

猜你喜欢

转载自blog.csdn.net/eadio/article/details/78981853