微信小程序调用百度文字转语音API

文字转语音,这里接入的是百度的https://ai.baidu.com/docs#/TTS-API/top里面有详细的介绍怎么去申请。不多说什么 直接效果已解决ios播放没有语音的bug,已经亲测魅族手机,iphone7手机
在这里插入图片描述
代码

export default {
	data() {
		return {
			token: '',//百度的token
		}
	},
	onLoad() {
		const innerAudioContext = wx.createInnerAudioContext()
	  const appkey = 'XtlaQgfm*******Io8BlzOs26'; //百度应用的 一个月过期
	  const selKey = 'fGwC9zQ*******D6ZkyzuN0yBOePC';//百度应用的 一个月过期 
	  uni.request({
	  	method:'GET',
		url:`https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id=${appkey}&client_secret=${selKey}`,
		header: {
			'content-type': 'application/json' // 默认值
		  },
		 success (res) {
			console.log(res.data)
			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('开始播放')
			})
		  }
	  })
	},
	methods: {
	}
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值