swan.ai.textToAudio
解释 :将文本转换为可以播放的 mp3 文件。
方法参数
Object object
data 参数说明
success 返回参数说明
示例
请使用百度APP扫码
图片示例
代码示例1
在开发者工具中预览效果 -
Page({ data: { sourceIndex: 5, sourceArray: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'], sizeIndex: 5, sizeArray: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'], countIndex: 5, countArray: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'], perIndex: 1, perArray: ['普通女声', '普通男声', '情感合成-度逍遥', '情感合成-度丫丫'], msg: 'hello,这是一段测试语音合成的文字' }, sourceChange(e) { this.setData('sourceIndex', e.detail.value); }, sizeChange(e) { this.setData('sizeIndex', e.detail.value); }, countChange(e) { this.setData('countIndex', e.detail.value); }, perChange(e) { this.setData('perIndex', e.detail.value); }, textToAudio() { let tex = this.getData('msg'); let sourceIndex = this.getData('sourceIndex'); let sizeIndex = this.getData('sizeIndex'); let countIndex = this.getData('countIndex'); let perIndex = this.getData('perIndex'); // AI系列的api有宿主使用限制,只可在百度App中使用,建议使用时加一层判断防止代码报未知错误 let host = swan.getSystemInfoSync().host; if (host === 'baiduboxapp') { swan.ai.textToAudio({ tex, ctp: '1', lan: 'zn', spd: JSON.stringify(this.data.sourceArray[sourceIndex]), pit: JSON.stringify(this.data.sizeArray[sizeIndex]), vol: JSON.stringify(this.data.countArray[countIndex]), per: JSON.stringify(this.data.perArray[perIndex]), success: res => { console.log('ai.textToAudio success', res); swan.showToast({ title: '合成成功', icon: 'none' }); }, fail: err => { console.log('ai.textToAudio fail', err); } }); } else { swan.showToast({ title: '此api目前仅可在百度App上使用', icon: 'none' }); } }});
代码示例2 - 普通女声
在开发者工具中预览效果 -
Page({ data: { }, textToAudio(){ // AI系列的api有宿主使用限制,只可在百度App中使用,建议使用时加一层判断防止代码报未知错误 let host = swan.getSystemInfoSync().host; if (host === 'baiduboxapp') { swan.ai.textToAudio({ tex: 'hello,这是一段测试语音合成的文字', ctp: '1', lan: 'zh', spd: '5', pit: '5', vol: '5', per: '0', success: res => { console.log('ai.textToAudio success', res); swan.showToast({ title: '合成成功', icon: 'none' }); }, fail: err => { console.log('ai.textToAudio fail', err); } }); } else { swan.showToast({ title: '此api目前仅可在百度App上使用', icon: 'none' }); } }});
代码示例3 - 普通男声
在开发者工具中预览效果 -
Page({ data: { }, textToAudio(){ // AI系列的api有宿主使用限制,只可在百度App中使用,建议使用时加一层判断防止代码报未知错误 let host = swan.getSystemInfoSync().host; if (host === 'baiduboxapp') { swan.ai.textToAudio({ tex: 'hello,这是一段测试语音合成的文字', ctp: '1', lan: 'zh', spd: '5', pit: '5', vol: '5', per: '1', success: res => { console.log('ai.textToAudio success', res); swan.showToast({ title: '合成成功', icon: 'none' }); }, fail: err => { console.log('ai.textToAudio fail', err); } }); } else { swan.showToast({ title: '此api目前仅可在百度App上使用', icon: 'none' }); } }});
代码示例4 - 情感合成-度逍遥
在开发者工具中预览效果 -
Page({ data: { }, textToAudio(){ // AI系列的api有宿主使用限制,只可在百度App中使用,建议使用时加一层判断防止代码报未知错误 let host = swan.getSystemInfoSync().host; if (host === 'baiduboxapp') { swan.ai.textToAudio({ tex: 'hello,这是一段测试语音合成的文字', ctp: '1', lan: 'zh', spd: '5', pit: '5', vol: '5', per: '3', success: res => { console.log('ai.textToAudio success', res); swan.showToast({ title: '合成成功', icon: 'none' }); }, fail: err => { console.log('ai.textToAudio fail', err); } }); } else { swan.showToast({ title: '此api目前仅可在百度App上使用', icon: 'none' }); } }});
代码示例5 - 情感合成-度丫丫
在开发者工具中预览效果 -
Page({ data: { }, textToAudio(){ // AI系列的api有宿主使用限制,只可在百度App中使用,建议使用时加一层判断防止代码报未知错误 let host = swan.getSystemInfoSync().host; if (host === 'baiduboxapp') { swan.ai.textToAudio({ tex: 'hello,这是一段测试语音合成的文字', ctp: '1', lan: 'zh', spd: '5', pit: '5', vol: '5', per: '4', success: res => { console.log('ai.textToAudio success', res); swan.showToast({ title: '合成成功', icon: 'none' }); }, fail: err => { console.log('ai.textToAudio fail', err); } }); } else { swan.showToast({ title: '此api目前仅可在百度App上使用', icon: 'none' }); } }});
Bug & Tip
重置 App Secret 会导致此功能无法使用。
