当前位置:   article > 正文

uni小程序中如何使用“百度语音”合成功能实现语音播报_百度语音播报接口

百度语音播报接口

提示:本案例是总结自己在实现小程序播放功能的一些操作,方便以后自己查找



前言

一、如何使用百度语音?

首先需要进入百度智能云注册一个账号并且创建一个自己的应用。

二、使用步骤

1.注册百度开发账号

百度智能云网址 登录注册完成进入当前页面
在这里插入图片描述

2.点击创建应用

拿到创建成功之后的 AppID、API Key、Secret Key(一会调用接口时会用到)
在这里插入图片描述


3.查看技术文档

因为我们这里需要的是将文字转成语音,所以用到的是语音合成技术

官网案列


用 postman 调用一下, 果然可以
  1. 获取百度平台返回的 token
    在这里插入图片描述
    2.调用语音合成接口
    在这里插入图片描述

4.小程序中接入

那么我们在小程序中要如何使用呢?

  • 首选需要将这两个地址添加到我们自己的微信小程序(request合法域名)
    地址:
    https://aip.baidubce.com;
    https://tsn.baidu.com;

5.小程序中播放

  • 注意代码要写到 onLoad 声明周期中
				const innerAudioContext = wx.createInnerAudioContext()
				wx.request({
					url: 'https://aip.baidubce.com/oauth/2.0/token',
					data: {
						grant_type: 'client_credentials',
						client_id: 'DjiQcGS*****',
						client_secret: 'oLyc0P8lWc****rwrToBGIn'
					},
					success: (res) => {
						if (wx.setInnerAudioOption) {
							wx.setInnerAudioOption({
								obeyMuteSwitch: false,
								autoplay: true
							})
						} else {
							innerAudioContext.obeyMuteSwitch = false;
							innerAudioContext.autoplay = true;
						}
						innerAudioContext.onCanplay(() => {
							console.log('可以播放');
						});
						innerAudioContext.onError((res) => {
							console.log(res.errMsg);
							console.log(res.errCode);
						});
						innerAudioContext.autoplay = true
						innerAudioContext.src = 'https://tsn.baidu.com/text2audio?lan=zh&ctp=1&cuid=abcdxxx&tok='+res.data.access_token+'&tex=' + encodeURIComponent(***需要转成语音的内容***) + '&vol=5&per=0&spd=5&pit=5&aue=3';
						innerAudioContext.onPlay(() => {
							console.log('开始播放')
						})
					}
				})
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/381509
推荐阅读
相关标签
  

闽ICP备14008679号