微信小程序语音播报

概要

需要用户一开始进入小程序的某个页面就进行语音播报

解决方案

使用微信小程序插件“微信同声传译”可以达到该功能

具体实现:

1. 微信公众平台=>设置=>第三方设置=>插件管理=>添加插件=>‘微信同音传译’=>添加(目前暂不支持个人开发者使用):

设置
插件

2、引入小程序插件:

2.1 复制插件AppID

基本信息

2.2 打开项目中的manifest.json=>源码视图=>添加如下代码:

插件

3、具体代码实现:

<template>
  <view class="content">
    <button @click="openVoice">播放语音</button>
  </view>
</template>
 
<script>
const plugin = requirePlugin('WechatSI');
const innerAudioContext = wx.createInnerAudioContext();
export default {
  data() {
    return {
      title: 'Hello'
    };
  },
  onLoad() {
        this.openVoice();
    },
  onReady() {
    innerAudioContext.onError(function (res) {
      wx.showToast({
        title: '语音播放失败',
        icon: 'none'
      });
    });
  },
  methods: {
    openVoice() {
      let _this = this;
      console.log(plugin);
      plugin.textToSpeech({
        lang: 'zh_CN',
        tts: true,
        content: '支付宝成功收款200万元',
        success: function (res) {
          console.log('succ tts', res.filename);
                    _this.yuyinPlay(res.filename)
        },
        fail: function (res) {
          console.log('fail tts', res);
        },
        complete: function (res) {
          console.log('complete tts', res);
        }
      });
    },
    yuyinPlay(src) {
      if (src == '') {
        return;
      }
      innerAudioContext.autoplay = true;
      innerAudioContext.src = src; //设置音频地址
      innerAudioContext.play(); //播放音频
    }
  }
};
</script>

4、如果播报的内容比较多的话,可以采取分段播放处理:

<template>
  <view class="content">
    <button @click="handleMoreText('支付宝成功收款200万元')">播放语音</button>
  </view>
</template>
 
<script>
const plugin = requirePlugin('WechatSI');
const innerAudioContext = wx.createInnerAudioContext();
export default {
  data() {
    return {
      title: 'Hello'
    };
  },
  onLoad() {
    this.handleMoreText('支付宝成功收款200万元');
  },
  onReady() {
    innerAudioContext.onError(function (res) {
      wx.showToast({
        title: '语音播放失败',
        icon: 'none'
      });
    });
  },
  methods: {
    /**
     * @description: 处理文本  同声传译一次最多1000字节,长文本按300字进行截断,然后按照朗读速度估算300字的时间,延迟下一次读取,正常300字/1分17秒
     * @param {*} content
     * @return {*}
     */
    handleMoreText(content) {
      let arrText = content.replace(/\r/g, ',').replace(/\n/g, ',').replace(/\s+/g, ',').replace(/#/g, ','); // 去除标点符号
      console.log(arrText, arrText.length); // 获取全文+总数字
      if (arrText.length < 300) {
        // 总数字小于300,直接转为语音
        this.openVoice(arrText);
      } else {
        // 总数字大于300,拆分成多个段落
        const num = Math.ceil(arrText.length / 300);
        const time = 75;
        for (let i = 0; i < num; i++) {
          const text = arrText.substr(0 + i * 300, 300); // 全文分成多个300字的段落
          setTimeout(() => {
            this.openVoice(text);
          }, time * 1000 * i); // 每隔1分17秒读一段
        }
      }
    },
    openVoice(content) {
      let _this = this;
      console.log(plugin);
      plugin.textToSpeech({
        lang: 'zh_CN',
        tts: true,
        content,
        success: function (res) {
          console.log('succ tts', res.filename);
          _this.yuyinPlay(res.filename);
        },
        fail: function (res) {
          console.log('fail tts', res);
        },
        complete: function (res) {
          console.log('complete tts', res);
        }
      });
    },
    yuyinPlay(src) {
      if (!src) return;
      innerAudioContext.autoplay = true;
      innerAudioContext.src = src; // 设置音频地址
      innerAudioContext.play(); // 播放音频
    }
  }
};
</script>
  • 6
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值