我需要使用文本到语音库。我决定使用https://responsivevoice.org/。然而,集成非常简单,因为我的项目使用GWT - 显然 - 不那么直接。
这是我的java代码,它是概念类的最小证明:
import com.google.gwt.core.client.Callback;
import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.ScriptInjector;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.*;
public class InverseVoiceTrainer extends SimplePanel implements ClickHandler {
Button playBtn;
public InverseVoiceTrainer() {
ScriptInjector.fromUrl("https://code.responsivevoice.org/responsivevoice.js").setCallback(
new Callback<Void, Exception>() {
@Override
public void onSuccess(Void result) {
GWT.log("ResponsiveVoiceJS loaded.");
}
@Override
public void onFailure(Exception reason) {
GWT.log("ResponsiveVoiceJS loading FAILED!");
}
}).inject();
playBtn = new Button("Play");
playBtn.addClickHandler(this);
this.add(playBtn);
}
@Override
public void onClick(ClickEvent event) {
GWT.log("Onclick pressed");
playWord("This is a test message...");
}
public static native void playWord(String s) /*-{
console.log("playWord - 1");
responsiveVoice.speak(s);
console.log("playWord - 2");
}-*/;
}
因此,查看控制台日志,我可以看到以下内容:
ResponsiveVoice r1.5.3
SuperDevModeLogger.java:71 ResponsiveVoiceJS loaded.
SuperDevModeLogger.java:71 Onclick pressed
InverseVoiceTrainer.java:40 playWord - 1
InverseVoiceTrainer.java:42 playWord - 2
这告诉我(a)ResponsiveVoice似乎已经正确加载并且(b)应该播放声音。但是,我没有听到任何声音,我的声音音量可以忍受。那么,这里出了什么问题?
答案 0 :(得分:1)
嗯,这可能不是最好的答案,但它对我有用。
首先,只需链接托管html页面中的 $.ajax({
method: 'post',
url: 'http://localhost:53101/api/values',
dataType: 'json',
data: {
// It takes key value pairs
ct1: {
num1: 1,
str1: 'some random string'
},
ct2: {
test2: 0.34
}
}
});
:
responsivevoice.js
然后删除代码的<script type="text/javascript" language="javascript" src="https://code.responsivevoice.org/responsivevoice.js"></script>
部分。
最后一项更改是在ScriptInjector
方法中添加$wnd
:
playWord()
测试。工作