创新互联百度小程序教程:swan.ai.textToAudio

  • swan.ai.textToAudio
    • 方法参数
      • object 参数说明
      • success 返回参数说明
    • 示例
      • 图片示例
      • 代码示例 1
      • 代码示例 2:普通女声
      • 代码示例 3:普通男声
      • 代码示例 4:情感合成—度逍遥
      • 代码示例 5:情感合成—度丫丫
    • Bug & Tip

    swan.ai.textToAudio

    解释:将文本转换为可以播放的 mp3 文件。
    Web 态说明:受宿主环境限制,Web 态暂不支持 AI 能力相关接口。在 Web 态会做 打开百度 App 对应小程序页面 的降级处理。

    成都创新互联主要业务有网站营销策划、成都网站设计、成都网站建设、外贸网站建设、微信公众号开发、小程序定制开发、H5高端网站建设、程序开发等业务。一次合作终身朋友,是我们奉行的宗旨;我们不仅仅把客户当客户,还把客户视为我们的合作伙伴,在开展业务的过程中,公司还积累了丰富的行业经验、营销型网站资源和合作伙伴关系资源,并逐渐建立起规范的客户服务和保障体系。 

    方法参数

    Object object

    object 参数说明

    属性名 类型 必填 默认值 说明

    tex

    String

    -

    合成的文本,使用 UTF-8 编码,小于 512 个中文字或者英文数字(文本在百度服务器内转换为 GBK 后,长度必须小于 1024 字节)

    ctp

    String

    1

    客户端类型选择, Web 端填写固定值 1

    lan

    String

    zh

    固定值 zh 。语言选择,目前只有中英文混合模式,填写固定值 zh

    spd

    String

    5

    语速,取值 0-9 ,默认为 5 中语速

    pit

    String

    5

    音调,取值 0-9 ,默认为 5 中语调

    vol

    String

    5

    音量,取值 0-9 ,默认为 5 中音量

    per

    String

    0

    发音人选择, 0 为普通女声, 1 为普通男生, 3 为情感合成-度逍遥, 4 为情感合成-度丫丫,默认为普通女声

    success

    Function

    接口调用成功的回调函数

    fail

    Function

    接口调用失败的回调函数

    complete

    Function

    接口调用结束的回调函数(调用成功、失败都会执行)

    success 返回参数说明

    参数 类型 说明

    filePath

    String

    合成的音频文件的路径,此路径为临时路径且在当次回调中有效

    示例

    扫码体验

    代码示例

    请使用百度APP扫码

    图片示例

    代码示例 1

    在开发者工具中打开

    在开发者工具中打开

    在 WEB IDE 中打开

    • JS
     
     
     
    1. Page({
    2. data: {
    3. sourceIndex: 5,
    4. sourceArray: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'],
    5. sizeIndex: 5,
    6. sizeArray: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'],
    7. countIndex: 5,
    8. countArray: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'],
    9. perIndex: 1,
    10. perArray: ['普通女声', '普通男声', '情感合成-度逍遥', '情感合成-度丫丫'],
    11. msg: 'hello,这是一段测试语音合成的文字'
    12. },
    13. sourceChange(e) {
    14. this.setData('sourceIndex', e.detail.value);
    15. },
    16. sizeChange(e) {
    17. this.setData('sizeIndex', e.detail.value);
    18. },
    19. countChange(e) {
    20. this.setData('countIndex', e.detail.value);
    21. },
    22. perChange(e) {
    23. this.setData('perIndex', e.detail.value);
    24. },
    25. textToAudio() {
    26. let tex = this.getData('msg');
    27. let sourceIndex = this.getData('sourceIndex');
    28. let sizeIndex = this.getData('sizeIndex');
    29. let countIndex = this.getData('countIndex');
    30. let perIndex = this.getData('perIndex');
    31. // AI系列的api有宿主使用限制,只可在百度App中使用,建议使用时加一层判断防止代码报未知错误
    32. let host = swan.getSystemInfoSync().host;
    33. if (host === 'baiduboxapp') {
    34. swan.ai.textToAudio({
    35. tex,
    36. ctp: '1',
    37. lan: 'zn',
    38. spd: JSON.stringify(this.data.sourceArray[sourceIndex]),
    39. pit: JSON.stringify(this.data.sizeArray[sizeIndex]),
    40. vol: JSON.stringify(this.data.countArray[countIndex]),
    41. per: JSON.stringify(perIndex),
    42. success: res => {
    43. console.log('ai.textToAudio success', res);
    44. swan.showToast({
    45. title: '合成成功',
    46. icon: 'none'
    47. });
    48. },
    49. fail: err => {
    50. console.log('ai.textToAudio fail', err);
    51. }
    52. });
    53. }
    54. else {
    55. swan.showToast({
    56. title: '此api目前仅可在百度App上使用',
    57. icon: 'none'
    58. });
    59. }
    60. }
    61. });

    代码示例 2:普通女声

    在开发者工具中打开

    在开发者工具中打开

    在 WEB IDE 中打开

    • JS
     
     
     
    1. Page({
    2. data: {
    3. },
    4. textToAudio(){
    5. // AI系列的api有宿主使用限制,只可在百度App中使用,建议使用时加一层判断防止代码报未知错误
    6. let host = swan.getSystemInfoSync().host;
    7. if (host === 'baiduboxapp') {
    8. swan.ai.textToAudio({
    9. tex: 'hello,这是一段测试语音合成的文字',
    10. ctp: '1',
    11. lan: 'zh',
    12. spd: '5',
    13. pit: '5',
    14. vol: '5',
    15. per: '0',
    16. success: res => {
    17. console.log('ai.textToAudio success', res);
    18. swan.showToast({
    19. title: '合成成功',
    20. icon: 'none'
    21. });
    22. },
    23. fail: err => {
    24. console.log('ai.textToAudio fail', err);
    25. }
    26. });
    27. }
    28. else {
    29. swan.showToast({
    30. title: '此api目前仅可在百度App上使用',
    31. icon: 'none'
    32. });
    33. }
    34. }
    35. });

    代码示例 3:普通男声

    在开发者工具中打开

    在开发者工具中打开

    在 WEB IDE 中打开

    • JS
     
     
     
    1. Page({
    2. data: {
    3. },
    4. textToAudio(){
    5. // AI系列的api有宿主使用限制,只可在百度App中使用,建议使用时加一层判断防止代码报未知错误
    6. let host = swan.getSystemInfoSync().host;
    7. if (host === 'baiduboxapp') {
    8. swan.ai.textToAudio({
    9. tex: 'hello,这是一段测试语音合成的文字',
    10. ctp: '1',
    11. lan: 'zh',
    12. spd: '5',
    13. pit: '5',
    14. vol: '5',
    15. per: '1',
    16. success: res => {
    17. console.log('ai.textToAudio success', res);
    18. swan.showToast({
    19. title: '合成成功',
    20. icon: 'none'
    21. });
    22. },
    23. fail: err => {
    24. console.log('ai.textToAudio fail', err);
    25. }
    26. });
    27. }
    28. else {
    29. swan.showToast({
    30. title: '此api目前仅可在百度App上使用',
    31. icon: 'none'
    32. });
    33. }
    34. }
    35. });

    代码示例 4:情感合成—度逍遥

    在开发者工具中打开

    在开发者工具中打开

    在 WEB IDE 中打开

    • JS
     
     
     
    1. Page({
    2. data: {
    3. },
    4. textToAudio(){
    5. // AI系列的api有宿主使用限制,只可在百度App中使用,建议使用时加一层判断防止代码报未知错误
    6. let host = swan.getSystemInfoSync().host;
    7. if (host === 'baiduboxapp') {
    8. swan.ai.textToAudio({
    9. tex: 'hello,这是一段测试语音合成的文字',
    10. ctp: '1',
    11. lan: 'zh',
    12. spd: '5',
    13. pit: '5',
    14. vol: '5',
    15. per: '3',
    16. success: res => {
    17. console.log('ai.textToAudio success', res);
    18. swan.showToast({
    19. title: '合成成功',
    20. icon: 'none'
    21. });
    22. },
    23. fail: err => {
    24. console.log('ai.textToAudio fail', err);
    25. }
    26. });
    27. }
    28. else {
    29. swan.showToast({
    30. title: '此api目前仅可在百度App上使用',
    31. icon: 'none'
    32. });
    33. }
    34. }
    35. });

    代码示例 5:情感合成—度丫丫

    在开发者工具中打开

    在开发者工具中打开

    在 WEB IDE 中打开

    • JS
     
     
     
    1. Page({
    2. data: {
    3. },
    4. textToAudio(){
    5. // AI系列的api有宿主使用限制,只可在百度App中使用,建议使用时加一层判断防止代码报未知错误
    6. let host = swan.getSystemInfoSync().host;
    7. if (host === 'baiduboxapp') {
    8. swan.ai.textToAudio({
    9. tex: 'hello,这是一段测试语音合成的文字',
    10. ctp: '1',
    11. lan: 'zh',
    12. spd: '5',
    13. pit: '5',
    14. vol: '5',
    15. per: '4',
    16. success: res => {
    17. console.log('ai.textToAudio success', res);
    18. swan.showToast({
    19. title: '合成成功',
    20. icon: 'none'
    21. });
    22. },
    23. fail: err => {
    24. console.log('ai.textToAudio fail', err);
    25. }
    26. });
    27. }
    28. else {
    29. swan.showToast({
    30. title: '此api目前仅可在百度App上使用',
    31. icon: 'none'
    32. });
    33. }
    34. }
    35. });

    Bug & Tip

    重置 App Secret 会导致此功能无法使用。

    网页题目:创新互联百度小程序教程:swan.ai.textToAudio
    网站网址:http://www.shufengxianlan.com/qtweb/news24/123774.html

    网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

    广告

    声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联