我需要使用文本语音库。我决定使用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)应该已播放声音。但是,我什么也没听到,而且音量是可以听到的。那么,这里出了什么问题?
最佳答案
好吧,这可能不是最佳答案,但对我有用。
首先,只需在托管html页面中链接responsivevoice.js
即可:
<script type="text/javascript" language="javascript" src="https://code.responsivevoice.org/responsivevoice.js"></script>
然后删除代码的
ScriptInjector
部分。最后的更改是在
$wnd
方法中添加playWord()
:$wnd.responsiveVoice.speak(s);
经过测试。工作了。