• JS实现文本的语音朗读


    更多内容请访问 www.uusystem.com

     
    摘要: 语音合成:也被称为文本转换技术(TTS),它是将计算机自己产生的、或外部输入的文字信息转变为可以听得懂的、流利的口语输出的技术。

    1、接口定义

    http://tts.baidu.com/text2audio?lan=zh&ie=UTF-8&spd=2&text=你要转换的文字

    参数说明:

    lan=zh:语言是中文,如果改为lan=en,则语言是英文。

    ie=UTF-8:文字格式。

    spd=2:语速,可以是1-9的数字,数字越大,语速越快。

    text=**:这个就是你要转换的文字。

    2、示例代码

     1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     2 <html>
     3     <head>
     4         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     5         <title>百度语音测试</title>
     6         <script type="text/javascript"> 
     7         function doTTS(){
     8             var ttsDiv = document.getElementById('bdtts_div_id');
     9             var ttsAudio = document.getElementById('tts_autio_id');
    10             var ttsText = document.getElementById('ttsText').value;
    11             
    12             // 这样为什么替换不了播放内容
    13             /*var ssrcc = 'http://tts.baidu.com/text2audio?lan=zh&ie=UTF-8&spd=10&text='+ttsText;
    14             document.getElementById('tts_source_id').src=ssrcc;*/
    15             
    16             // 这样就可实现播放内容的替换了
    17             ttsDiv.removeChild(ttsAudio);
    18             var au1 = '<audio id="tts_autio_id" autoplay="autoplay">';
    19             var sss = '<source id="tts_source_id" src="http://tts.baidu.com/text2audio?lan=zh&ie=UTF-8&spd=9&text='+ttsText+'" type="audio/mpeg">';
    20             var eee = '<embed id="tts_embed_id" height="0" width="0" src="">';
    21             var au2 = '</audio>';
    22             ttsDiv.innerHTML = au1 + sss + eee + au2;
    23             
    24             ttsAudio = document.getElementById('tts_autio_id');
    25             
    26             ttsAudio.play();
    27         }
    28         </script>
    29     </head>
    30     <body>
    31         <div>
    32             <input type="text" id="ttsText">
    33             <input type="button" id="tts_btn" onclick="doTTS()" value="播放">
    34         </div>
    35         <div id="bdtts_div_id">
    36             <audio id="tts_autio_id" autoplay="autoplay">
    37                 <source id="tts_source_id" src="http://tts.baidu.com/text2audio?lan=zh&ie=UTF-8&spd=9&text=播报内容" type="audio/mpeg">
    38                 <embed id="tts_embed_id" height="0" width="0" src="">
    39             </audio>
    40         </div>
    41     </body>
    42 </html>

    转发于:https://blog.csdn.net/mixi9760/article/details/76206926

  • 相关阅读:
    Treap 树堆 容易实现的平衡树
    (转)Maven实战(二)构建简单Maven项目
    (转)Maven实战(一)安装与配置
    根据请求头跳转判断Android&iOS
    (转)苹果消息推送服务器 php 证书生成
    (转)How to renew your Apple Push Notification Push SSL Certificate
    (转)How to build an Apple Push Notification provider server (tutorial)
    (转)pem, cer, p12 and the pains of iOS Push Notifications encryption
    (转)Apple Push Notification Services in iOS 6 Tutorial: Part 2/2
    (转)Apple Push Notification Services in iOS 6 Tutorial: Part 1/2
  • 原文地址:https://www.cnblogs.com/tianjifa/p/10443133.html
Copyright © 2020-2023  润新知