如何听到文本到语音翻译的音频

时间:2010-10-04 06:39:13

标签: html html5 audio text-to-speech

当我点击图像(例如音频图像)时,我希望听到音频。句子的声音文件是通过谷歌翻译获得的。我想这样做而不会被重定向到另一个页面。我该怎么做?我尝试使用HTML5音频标签,但也没有成功。

<html>
<body>
<a href="http://translate.google.com/translate_tts?q=My+name+is+John">My name is John</a>

</body>
</html>

1 个答案:

答案 0 :(得分:3)

<强> EDIT2

此处的another solution也发布在SO上,可能对您有帮助。


修改

更强大的解决方案。在IE8和Firefox中测试

<html>
  <head>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" ></script>
  </head>
  <body>
    <img src="http://www.blackwellokradio.com/click-me.jpg" onclick="$.sound.play('http://translate.google.com/translate_tts?q=My+name+is+John')" style="cursor:hand;cursor:pointer;" />
  </body>
</html>     
<!--Ideally you would put this in a separate file. src: http://dev.jquery.com/browser/trunk/plugins/sound/jquery.sound.js?rev=5750-->
<script type="text/javascript"> 
/**
 * jQuery sound plugin (no flash)
 * 
 * port of script.aculo.us' sound.js (http://script.aculo.us), based on code by Jules Gravinese (http://www.webveteran.com/) 
 * 
 * Copyright (c) 2007 Jörn Zaefferer (http://bassistance.de) 
 * 
 * Licensed under the MIT license:
 *   http://www.opensource.org/licenses/mit-license.php
 *   
 * $Id$
 */

/**
 * API Documentation
 * 
 * // play a sound from the url
 * $.sound.play(url)
 * 
 * // play a sound from the url, on a track, stopping any sound already running on that track
 * $.sound.play(url, {
 *   track: "track1"
 * });
 * 
 * // increase the timeout to four seconds before removing the sound object from the dom for longer sounds
 * $.sound.play(url, {
 *   timeout: 4000
 * });
 * 
 * // stop a sound by removing the element returned by play
 * var sound = $.sound.play(url);
 * sound.remove();
 * 
 * // disable playing sounds
 * $.sound.enabled = false;
 * 
 * // enable playing sounds
 * $.sound.enabled = true
 */

(function($) {

$.sound = {
    tracks: {},
    enabled: true,
    template: function(src) {
        return '<embed style="height:0" loop="false" src="' + src + '" autostart="true" hidden="true"/>';
    },
    play: function(url, options){
        if (!this.enabled)
            return;
        var settings = $.extend({
            url: url,
            timeout: 2000
        }, options);

        if (settings.track) {
            if (this.tracks[settings.track]) {
                var current = this.tracks[settings.track];
                // TODO check when Stop is avaiable, certainly not on a jQuery object
                current.Stop && current.Stop();
                current.remove();  
            }
        }

        var element = $.browser.msie
            ? $('<bgsound/>').attr({
                src: settings.url,
                loop: 1,
                autostart: true
              })
            : $(this.template(settings.url));

        element.appendTo("body");

        if (settings.track) {
            this.tracks[settings.track] = element;
        }

        if(options){
            setTimeout(function() {
                element.remove();
            }, options.timeout)
        }

        return element;
    }
};

})(jQuery);
</script>

这是一种嵌入Quicktime的方法。您可以轻松使用Windows Media Player ...

<html>
  <head>
  </head>
  <body>
    <object classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" codebase="http://www.apple.com/qtactivex/qtplugin.cab" width="300" height="50">
        <param name="src" value="http://translate.google.com/translate_tts?q=My+name+is+John">
        <param name="autoplay" value="true">
        <embed type="audio/x-wav" src="http://translate.google.com/translate_tts?q=My+name+is+John" autoplay="true" autostart="true" width="300" height="50">
    </object>
  </body>
</html>