簡體   English   中英

HTML5音頻錯誤

[英]Error with HTML5 Audio

我正在嘗試使用Google翻譯URL服務進行TTS。

以下是代碼Javascript代碼。

function longSentenceSpeak(text)
{
    var string = text.trim().split(" ").join("+");
    var finalString = string.replace(";",",")
    var url = "http://www.translate.google.com/translate_tts?tl=en&q="+finalString;

    var audio = document.getElementById('audio');

    var source = document.getElementById('source');
    source.src=url;

    audio.load(); //call this to just preload the audio without playing
    audio.play(); //call this to play the song right away
}

以下是我的HTML

TODO提供標題

    <script src="scripts/TTS.js"></script>
    <script>
       function longText()
       {
           longSentenceSpeak("hello world ");
       }
    </script>
</head>
<body>
    <audio id="audio">
        <source id="source" src="" type="audio/wav" />
    </audio>

    <div><button onclick="longText()">Click me</button></div>
</body>

但是,這根本行不通,似乎HTML 5音頻無法處理URL。 當我運行此代碼時,出現以下錯誤。

Failed to load resource: the server responded with a status of 404 (Not Found) (12:49:15:455 | error, network)
  at http://www.translate.google.com/translate_tts?tl=en&q=hello

但我確定網址正確無誤,因為您可以嘗試這樣做

http://www.translate.google.com/translate_tts?tl=en&q=hello%20world

以下是JS小提琴鏈接

http://jsfiddle.net/f1hgk5sc/

我在這里做錯了什么?

好吧,我只是復制了您的代碼,所以一切正常。

 function longSentenceSpeak(text) { var string = text.trim().split(" ").join("+"); var finalString = string.replace(";", ",") var url = "http://www.translate.google.com/translate_tts?tl=en&q=" + finalString; var audio = document.getElementById('audio'); var source = document.getElementById('source'); source.src = url; audio.load(); //call this to just preload the audio without playing audio.play(); //call this to play the song right away } 
 <html> <head> <script> function longText() { longSentenceSpeak("hello world "); } </script> </head> <body> <audio id="audio"> <source id="source" src="" type="audio/wav" /> </audio> <div> <button onclick="longText()">Click me</button> </div> </body> </html> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM