Google Text to Speech Api 点击播放网站

Google Text to Speech Api on click play website

我有这一段

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.

并想在我的网站上 播放 ! 到目前为止,这是我最好的代码,他们说它不能处理超过 100 个字符。

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

有什么建议吗?仅使用 jquery / Javascript / PHP 谢谢

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");

else部分代码

$("#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!
            }
        }
    });
});

php 是这样的:(你可以把它放在同一个页面里(在顶部)!)

<?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;
}
?>

要将字符串正确拆分为多个部分,您可以查找类似于 Split string into sentences in javascript

的问题