如何使用 javascript 在点击时播放 google tts

发布于 2024-09-19 18:31:18 字数 346 浏览 3 评论 0原文

我正在尝试编写一个可以说出某些文本的js函数,就像这样”

function SpeakText(text) {
......
}

我发现了这个步骤: 如何创建一个播放 mp3 google tts 的按钮,这似乎可以满足我的要求。但是我尝试了解决方案,但不起作用。也许它需要html5 可以工作吗?

有一个简单的 javascript 函数可以简单地说出我输入的某些文本吗?

I'm trying to write a js function that can speak certain text, like this"

function SpeakText(text) {
......
}

I found this tread: how to create a button that plays a mp3 google tts that seems to do what I want. But I tried the solutions but doesn't work. Maybe it requires html5 to work?

Is there a simple javascript function that can simply speak certain text that I input to it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不疑不惑不回忆 2024-09-26 18:31:18

老线程,但我的回答可能对某些人有帮助。请记住 Google 的 TTS 许可方法并安装 VLC 播放器作为输出机制。
干杯。
梅赫梅特

HTML:

 <div    id="divVLCA2"       style="border: 0px solid black; background-color: transparent; left:   0px;  top:     0px; width: 1px;  height:  1px;    position: absolute; overflow: hidden">
       <embed id="vlca2"
                   width="1"               height="1"
                   type="application/x-vlc-plugin"       pluginspage="http://www.videolan.org"
                   autoplay="false"        controls="false"/>
 </div>

JS:

 var q_UNICODE_MAIL            = "UTF-8";
 var q_SLASH                   = "/";
 var q_TTS_URL                 = "ttsServlet";
 var q_GOOGLE_IE               = "ie=";                         
 var q_GOOGLE_TL               = "tl=";                  
 var q_GOOGLE_Q                = "q=";                
 var q_LANG_EN                 = "EN";               

 function SpeakText(x_word) {
     var divVLCA2                = document.getElementById("divVLCA2");
     divVLCA2  .style.visibility = "visible"; divVLCA2  .style.display = "block";

var vlca2                   = document.getElementById("vlca2");
var w_options               = new Array("");
var w_word                  = encodeURIComponent(x_word,         q_UNICODE_MAIL);
var w_parms_a               =                    q_GOOGLE_IE +   q_UNICODE_MAIL
                                + "&"          + q_GOOGLE_TL +   q_LANG_EN
                                + "&"          + q_GOOGLE_Q  +   w_word;
var w_servlet_a             = "http://your_server:8080" + q_SLASH      + q_TTS_URL   + "?" + w_parms_a;
vlca2.playlist.clear();
vlca2.playlist.add(w_servlet_a, null, w_options);
vlca2.playlist.play();
 }

SERVLET (ttsServlet) :

 import com.gtranslate.*;

 //servlet output: "audio/x-wav";
 //...



public void mainXML(HttpServletRequest request, HttpServletResponse response) throws Exception {
 public static String     q_UNICODE_MAIL           = "UTF-8";
 int                      q_YAYIN_SOCKET_ABUFFER_UZN= 20480;
  //-------------------------------------------------------------------------------------------------------------//
  //                                            P A R A M E T E R S                                              //
  //-------------------------------------------------------------------------------------------------------------//
    String x_ie        = request.getParameter("ie"); if ( x_ie   == null ) {x_ie   = "";}
    String x_lang      = request.getParameter("tl"); if ( x_lang == null ) {x_lang = "";}
    String x_word      = request.getParameter("q");  if ( x_word == null ) {x_word = "";}
  //-------------------------------------------------------------------------------------------------------------//

    String word        = URLEncoder.encode(x_word, q_UNICODE_MAIL);

    Audio               audio       = Audio.getInstance();
    InputStream         sound       = audio.getAudio(word, x_lang);
    ServletOutputStream x_sos       = response.getOutputStream();

    try {
        byte[] bytes        = new byte[q_YAYIN_SOCKET_ABUFFER_UZN];
        int    nBytesRead   = 0;
        nBytesRead          = sound.read(bytes, 0, bytes.length);
        while    ( nBytesRead > 0 ) {
            try {
                  x_sos.write(bytes, 0, nBytesRead);
                  nBytesRead= sound.read(bytes, 0, bytes.length);
            } catch (Exception e) {
                  System.out.println("> ERROR (TTS-1) : " + e.getMessage());
                  break;
            }
        }
    } catch (Exception e) {
        System.out.println("> ERROR (TTS-2) : " + e.getMessage());
    }



    sound.close();
}

Old thread but my answer may be helpful for some people. Please remember Google's TTS license approach and installing VLC Player as output mechanism.
Cheers.
mehmet

HTML:

 <div    id="divVLCA2"       style="border: 0px solid black; background-color: transparent; left:   0px;  top:     0px; width: 1px;  height:  1px;    position: absolute; overflow: hidden">
       <embed id="vlca2"
                   width="1"               height="1"
                   type="application/x-vlc-plugin"       pluginspage="http://www.videolan.org"
                   autoplay="false"        controls="false"/>
 </div>

JS:

 var q_UNICODE_MAIL            = "UTF-8";
 var q_SLASH                   = "/";
 var q_TTS_URL                 = "ttsServlet";
 var q_GOOGLE_IE               = "ie=";                         
 var q_GOOGLE_TL               = "tl=";                  
 var q_GOOGLE_Q                = "q=";                
 var q_LANG_EN                 = "EN";               

 function SpeakText(x_word) {
     var divVLCA2                = document.getElementById("divVLCA2");
     divVLCA2  .style.visibility = "visible"; divVLCA2  .style.display = "block";

var vlca2                   = document.getElementById("vlca2");
var w_options               = new Array("");
var w_word                  = encodeURIComponent(x_word,         q_UNICODE_MAIL);
var w_parms_a               =                    q_GOOGLE_IE +   q_UNICODE_MAIL
                                + "&"          + q_GOOGLE_TL +   q_LANG_EN
                                + "&"          + q_GOOGLE_Q  +   w_word;
var w_servlet_a             = "http://your_server:8080" + q_SLASH      + q_TTS_URL   + "?" + w_parms_a;
vlca2.playlist.clear();
vlca2.playlist.add(w_servlet_a, null, w_options);
vlca2.playlist.play();
 }

SERVLET (ttsServlet) :

 import com.gtranslate.*;

 //servlet output: "audio/x-wav";
 //...



public void mainXML(HttpServletRequest request, HttpServletResponse response) throws Exception {
 public static String     q_UNICODE_MAIL           = "UTF-8";
 int                      q_YAYIN_SOCKET_ABUFFER_UZN= 20480;
  //-------------------------------------------------------------------------------------------------------------//
  //                                            P A R A M E T E R S                                              //
  //-------------------------------------------------------------------------------------------------------------//
    String x_ie        = request.getParameter("ie"); if ( x_ie   == null ) {x_ie   = "";}
    String x_lang      = request.getParameter("tl"); if ( x_lang == null ) {x_lang = "";}
    String x_word      = request.getParameter("q");  if ( x_word == null ) {x_word = "";}
  //-------------------------------------------------------------------------------------------------------------//

    String word        = URLEncoder.encode(x_word, q_UNICODE_MAIL);

    Audio               audio       = Audio.getInstance();
    InputStream         sound       = audio.getAudio(word, x_lang);
    ServletOutputStream x_sos       = response.getOutputStream();

    try {
        byte[] bytes        = new byte[q_YAYIN_SOCKET_ABUFFER_UZN];
        int    nBytesRead   = 0;
        nBytesRead          = sound.read(bytes, 0, bytes.length);
        while    ( nBytesRead > 0 ) {
            try {
                  x_sos.write(bytes, 0, nBytesRead);
                  nBytesRead= sound.read(bytes, 0, bytes.length);
            } catch (Exception e) {
                  System.out.println("> ERROR (TTS-1) : " + e.getMessage());
                  break;
            }
        }
    } catch (Exception e) {
        System.out.println("> ERROR (TTS-2) : " + e.getMessage());
    }



    sound.close();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文