doubi9999 2015-04-09 07:34
浏览 72
已采纳

点击播放网站上的Google Text to Speech Api

i have this paragraph

A tattoo of angel wings on the back of the young girl pointed officers to a runaway, a 15-year-old school girl called Tina Fontaine. Within days Tina's case was making headlines throughout Canada not just for the horrific nature of her death, but for what she had come to represent.

and want to play it on my website on click ! so far this is my best code, they say it cant handle more than 100 characters.

var audio = new Audio(); 
audio.src ='http://translate.google.com/translate_tts?ie=utf-8&tl=en&q=Hello%20World.'; 
audio.play();

Any suggesions ? using only jquery / Javascript / PHP Thankyou

  • 写回答

1条回答 默认 最新

  • dosgy00204 2015-04-09 07:53
    关注
    function say( text ){
      if('speechSynthesis' in window) { // Chrome only !!
    
        var speech = new SpeechSynthesisUtterance( text );
        speech.lang = 'en-US';
        window.speechSynthesis.speak(speech);
    
      } else { // Other browsers !!
    
         // Use AJAX (with GET) to a .php to file_get_contents
         // generate the <100 by <100 charaters audio files, and nest in callbacks
    
      }
    }
    
    
    say("A tattoo of angel wings can handle more than 100 characters");
    

    The else part of the code

    $("#playButton").click(function(){
        var string = encodeURIComponent( $("textarea").val() );
        // TODO: split 100+ string into chunks of rightly punctuated sentences.
        $.ajax({
            data: { text: string },
            success : function(d) {
                var audio = new Audio();
                audio.src = "data:audio/mpeg;base64,"+d;
                audio.play();
                audio.onended = function() {
                    alert("Ended playing first part");
                    // TODO : if we have chunks, play the next one!
                }
            }
        });
    });
    

    The php goes like: (you can put it inside the same page (at the top)!)

    <?php 
    if ($_SERVER['REQUEST_METHOD'] === 'GET' && isset($_GET['text']) && isset($_GET['lang'])) {
        $voice = file_get_contents("http://translate.google.com/translate_tts?q=". $_GET['text'] ."&tl=". $_GET['lang'] ."&ie=UTF-8");
        echo base64_encode( $voice );
        exit;
    }
    ?>
    

    To properly split the string into parts you can look for questions similar to Split string into sentences in javascript

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝