来自Android应用的谷歌文字转语音API [英] Google's Text-To-Speech API from Android app
问题描述
我想在Android应用中使用Google的文字转语音API,但我只能从网络(Chrome)中找到解决方法。这是我第一次尝试从应用程序中播放Hello world。
I want to use Google's Text-To-Speech API in an Android app but I only could find the way to do it from web (Chrome). This is my first attempt to play "Hello world" from the app.
playTTS
是onClick,它是已执行,但没有播放声音。我需要导入任何JS / Java库吗?是否可以从中生成音频文件?
playTTS
is the onClick and it is been executed, but no sound is played. Is there any JS/Java library I need to import? Is it possible to generate an audio file from it?
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myBrowser = new WebView(this);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
public void playTTS(View view) {
myBrowser.loadUrl("javascript:speechSynthesis.speak(
SpeechSynthesisUtterance('Hello World'))");
}
推荐答案
在Android的java代码中你的活动/其他类应实现 TextToSpeech.OnInitListener
。您将通过调用 TextToSpeech(context,this)
获得 TextToSpeech
实例。 (其中 context
指的是你的应用程序的上下文 - 在一个Activity中可以这个
。)然后你会收到一个 onInit()
回调 status
,它告诉TTS引擎是否可用。
In Android java code your Activity/other Class should implement TextToSpeech.OnInitListener
. You will get a TextToSpeech
instance by calling TextToSpeech(context, this)
. (Where context
refers to your application's Context -- can be this
in an Activity.) You will then receive a onInit()
callback with status
which tells whether the TTS engine is available or not.
您可以通过调用 tts.speak(textToBeSpoken,TextToSpeech.QUEUE_FLUSH,null)
或 tts.speak(textToBeSpoken, TextToSpeech.QUEUE_ADD,null)
。第一个将中断任何仍在讲话的话语,后者将把新的话语添加到队列中。最后一个参数不是必需的。如果您想通过设置 UtteranceProgressListener
来监控TTS状态,它可能是您定义的话语ID。 (没必要)
You can talk by calling tts.speak(textToBeSpoken, TextToSpeech.QUEUE_FLUSH, null)
or tts.speak(textToBeSpoken, TextToSpeech.QUEUE_ADD, null)
. The first one will interrupt any "utterance" that is still being spoken and the latter one will add the new "utterance" to a queue. The last parameter is not mandatory. It could be an "utterance id" defined by you in case you want to monitor the TTS status by setting an UtteranceProgressListener
. (Not necessary)
在Java代码中,一个简单的TTS talker类可能是这样的:
In Java code a simple "TTS talker" class could be something like:
public class MyTtsTalker implements TextToSpeech.OnInitListener {
private TextToSpeech tts;
private boolean ttsOk;
// The constructor will create a TextToSpeech instance.
MyTtsTalker(Context context) {
tts = new TextToSpeech(context, this);
}
@Override
// OnInitListener method to receive the TTS engine status
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
ttsOk = true;
}
else {
ttsOk = false;
}
}
// A method to speak something
@SuppressWarnings("deprecation") // Support older API levels too.
public void speak(String text, Boolean override) {
if (ttsOk) {
if (override) {
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}
else {
tts.speak(text, TextToSpeech.QUEUE_ADD, null);
}
}
}
}
这篇关于来自Android应用的谷歌文字转语音API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!