赞
踩
提示:本案例是总结自己在实现小程序播放功能的一些操作,方便以后自己查找
首先需要进入百度智能云注册一个账号并且创建一个自己的应用。
百度智能云网址 登录注册完成进入当前页面
拿到创建成功之后的 AppID、API Key、Secret Key(一会调用接口时会用到)
因为我们这里需要的是将文字转成语音,所以用到的是语音合成技术
那么我们在小程序中要如何使用呢?
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('开始播放') }) } })
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。