zoukankan      html  css  js  c++  java
  • [js常用]百度将文字转化为语音实例

    嗷嗷方便的文字转语音,不过用的时候记得到百度语音上申请key,免费的.之前在网络上看到有人写了一部分,自己丰富下,以后用也方便

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
      <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>百度语音测试</title>
      </head>
      <body>
        <div>
          <input type="text" id="ttsText">
          <input type="button" id="tts_btn" onclick="doTTS()" value="获取新的语音">
          <input type="button" id="tts_btns" onclick="zanting()" value="暂停">
          <input type="button" id="tts_btns" onclick="jixu()" value="继续/播放">
           <input type="button" id="tts_btns" onclick="shichang()" value="时长">
        </div>
        <div id="bdtts_div_id">
          <audio id="tts_autio_id" >
            <source id="tts_source_id" src="https://tsn.baidu.com/text2audio?tex=我喜欢你&lan=zh&per=4&cuid=1&ctp=1&tok=24.71726d18e11bf62415c11e3c2aa88121.2592000.1544946462.282335-14837008" type="audio/mpeg">
            <embed id="tts_embed_id" height="0" width="0" src="">
          </audio>
        </div>
      </body>
    </html>
    <script>
    
        function zanting() {
            var ttsAudio = document.getElementById('tts_autio_id');
            ttsAudio.pause();
        }
        function jixu() {
            var ttsAudio = document.getElementById('tts_autio_id');
            ttsAudio.play();
        }
    
        function shichang() {
              var ttsAudio = document.getElementById('tts_autio_id');
              alert(ttsAudio.duration);
        }
    
            function doTTS(){
          var ttsDiv = document.getElementById('bdtts_div_id');
          var ttsAudio = document.getElementById('tts_autio_id');
          var ttsText = document.getElementById('ttsText').value;
          
          // 这样为什么替换不了播放内容
          /*var ssrcc = 'http://tts.baidu.com/text2audio?lan=zh&ie=UTF-8&spd=10&text='+ttsText;
          document.getElementById('tts_source_id').src=ssrcc;*/
          
          // 这样就可实现播放内容的替换了
          ttsDiv.removeChild(ttsAudio);
          var au1 = '<audio id="tts_autio_id" autoplay="autoplay">';
          var sss = '<source id="tts_source_id" src="https://tsn.baidu.com/text2audio?tex='+ttsText+'&lan=zh&per=4&cuid=1&ctp=1&tok=24.71726d18e11bf62415c11e3c2aa88121.2592000.1544946462.282335-14837008" type="audio/mpeg">';
          var eee = '<embed id="tts_embed_id" height="0" width="0" src="">';
          var au2 = '</audio>';
          ttsDiv.innerHTML = au1 + sss + eee + au2;
          
          ttsAudio = document.getElementById('tts_autio_id');
          
          ttsAudio.play();
        }
    
    
    
    
    </script>
  • 相关阅读:
    10-23C#基础--结构体
    10-23C#基础--特殊集合(stack、queue、hashtable)
    10-21C#基础--集合
    10-20C#基础---一维、二维数组&&冒泡排序
    10-19C#基础--第四部分类型(2)重点
    10-17C#第四部分--类型(1)
    10-17C#语句(3)--跳转语句、异常处理语句
    10-16C#for...循环语句(2)
    C# DEBUG 调试信息打印及输出详解
    .NET中值得体验的精妙设计
  • 原文地址:https://www.cnblogs.com/lovetangyuxian/p/10109162.html
Copyright © 2011-2022 走看看