我正在尝试编写一个可以说某些文本的js函数,比如“
function SpeakText(text) {
......
}
我发现这一点:how to create a button that plays a mp3 google tts似乎做了我想做的事。但我尝试了解决方案,但没有奏效。也许它需要html5才能工作?
是否有一个简单的javascript函数可以简单地说出我输入的某些文本?
答案 0 :(得分:0)
旧帖子但我的回答可能对某些人有帮助。请记住Google的TTS许可方法并安装VLC Player作为输出机制。 干杯。 穆罕默德
<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>
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();
}
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();
}