文字转语音,这里接入的是百度的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: {
}
}