谷歌'的文本到语音API从Android应用程序
Google's Text-To-Speech API from Android app
我想在Android应用程序中使用谷歌的文本到语音API,但我只能找到从web (Chrome)做到这一点的方法。这是我第一次尝试从应用程序中播放"Hello world"。
playTTS
为onClick,已执行,但未播放声音。是否有任何JS/Java库我需要导入?有可能从中生成音频文件吗?
@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
指的是你的应用程序的上下文-可以是this
在一个活动。)然后,您将收到一个onInit()
回调,status
告诉您TTS引擎是否可用。
您可以通过呼叫tts.speak(textToBeSpoken, TextToSpeech.QUEUE_FLUSH, null)
或tts.speak(textToBeSpoken, TextToSpeech.QUEUE_ADD, null)
通话。第一个程序将打断任何仍在说话的"话语",后一个程序将把新的"话语"添加到队列中。最后一个参数不是必选参数。如果您想通过设置UtteranceProgressListener
来监视TTS状态,它可以是您定义的"话语id"。(没必要)
在Java代码中,一个简单的"TTS talker"类可以是这样的:
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);
}
}
}
}
TTS代码:
import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.speech.tts.TextToSpeech;
import android.speech.tts.UtteranceProgressListener;
import android.util.Log;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
import android.widget.Toast;
import java.util.HashMap;
import java.util.Locale;
public class KiwixTextToSpeech {
public static final String TAG_ASKQ = "askq;
private Context context;
private OnSpeakingListener onSpeakingListener;
private WebView webView;
private TextToSpeech tts;
private boolean initialized = false;
/**
* Constructor.
*
* @param context the context to create TextToSpeech with
* @param webView {@link android.webkit.WebView} to take contents from
* @param onInitSucceedListener listener that receives event when initialization of TTS is done
* (and does not receive if it failed)
* @param onSpeakingListener listener that receives an event when speaking just started or
* ended
*/
public KiwixTextToSpeech(Context context, WebView webView,
final OnInitSucceedListener onInitSucceedListener,
final OnSpeakingListener onSpeakingListener) {
Log.d(TAG_ASKQ, "Initializing TextToSpeech");
this.context = context;
this.onSpeakingListener = onSpeakingListener;
this.webView = webView;
this.webView.addJavascriptInterface(new TTSJavaScriptInterface(), "tts");
initTTS(onInitSucceedListener);
}
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1)
private void initTTS(final OnInitSucceedListener onInitSucceedListener) {
tts = new TextToSpeech(context, new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
Log.d(TAG_ASKQ, "TextToSpeech was initialized successfully.");
initialized = true;
onInitSucceedListener.onInitSucceed();
} else {
Log.e(TAG_ASKQ, "Initilization of TextToSpeech Failed!");
}
}
});
tts.setOnUtteranceProgressListener(new UtteranceProgressListener() {
@Override
public void onStart(String utteranceId) {
}
@Override
public void onDone(String utteranceId) {
Log.e(TAG_ASKQ, "TextToSpeech: " + utteranceId);
onSpeakingListener.onSpeakingEnded();
}
@Override
public void onError(String utteranceId) {
Log.e(TAG_ASKQ, "TextToSpeech: " + utteranceId);
onSpeakingListener.onSpeakingEnded();
}
});
}
/**
* Reads the currently selected text in the WebView.
*/
public void readSelection() {
webView.loadUrl("javascript:tts.speakAloud(window.getSelection().toString());", null);
}
/**
* Starts speaking the WebView content aloud (or stops it if TTS is speaking now).
*/
public void readAloud() {
if (tts.isSpeaking()) {
if (tts.stop() == TextToSpeech.SUCCESS) {
onSpeakingListener.onSpeakingEnded();
}
} else {
Locale locale = LanguageUtils.ISO3ToLocale(ZimContentProvider.getLanguage());
int result;
if (locale == null
|| (result = tts.isLanguageAvailable(locale)) == TextToSpeech.LANG_MISSING_DATA
|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
Log.d(TAG_ASKQ, "TextToSpeech: language not supported: " +
ZimContentProvider.getLanguage() + " (" + locale.getLanguage() + ")");
Toast.makeText(context,
context.getResources().getString(R.string.tts_lang_not_supported),
Toast.LENGTH_LONG).show();
} else {
tts.setLanguage(locale);
// We use JavaScript to get the content of the page conveniently, earlier making some
// changes in the page
webView.loadUrl("javascript:" +
"body = document.getElementsByTagName('body')[0].cloneNode(true);" +
// Remove some elements that are shouldn't be read (table of contents,
// references numbers, thumbnail captions, duplicated title, etc.)
"toRemove = body.querySelectorAll('sup.reference, #toc, .thumbcaption, " +
" title, .navbox');" +
"Array.prototype.forEach.call(toRemove, function(elem) {" +
" elem.parentElement.removeChild(elem);" +
"});" +
"tts.speakAloud(body.innerText);");
}
}
}
/**
* Returns whether the TTS is initialized.
*
* @return <code>true</code> if TTS is initialized; <code>false</code> otherwise
*/
public boolean isInitialized() {
return initialized;
}
/**
* Releases the resources used by the engine.
*
* @see android.speech.tts.TextToSpeech#shutdown()
*/
public void shutdown() {
tts.shutdown();
}
/**
* The listener which is notified when initialization of the TextToSpeech engine is successfully
* done.
*/
public interface OnInitSucceedListener {
public void onInitSucceed();
}
/**
* The listener that is notified when speaking starts or stops (regardless of whether it was a
* result of error, user, or because whole text was read).
*
* Note that the methods of this interface may not be called from the UI thread.
*/
public interface OnSpeakingListener {
public void onSpeakingStarted();
public void onSpeakingEnded();
}
private class TTSJavaScriptInterface {
@JavascriptInterface
@SuppressWarnings("unused")
public void speakAloud(String content) {
String[] lines = content.split("'n");
for (int i = 0; i < lines.length - 1; i++) {
String line = lines[i];
tts.speak(line, TextToSpeech.QUEUE_ADD, null);
}
HashMap<String, String> params = new HashMap<>();
// The utterance ID isn't actually used anywhere, the param is passed only to force
// the utterance listener to be notified
params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "kiwixLastMessage");
tts.speak(lines[lines.length - 1], TextToSpeech.QUEUE_ADD, params);
if (lines.length > 0) {
onSpeakingListener.onSpeakingStarted();
}
}
}
}
相关文章:
- 网络语音API-再次讲话(重置句子)
- 网络语音API在商业应用中的应用
- 从语音识别api结果中删除以前识别的单词
- Web 语音 API:在 iOS 野生动物园上持续获取支持的语音合成语音
- 2按钮,形式与语音api相同
- angular js文本到语音转换api
- jquery移动phonegap应用程序的语音到文本api
- 禁用通过Web语音API进行识别的污言秽语过滤器
- 节点 Webkit 文本到语音 API 不起作用
- 如何在 Web 语音 API 中继续识别,而无需重复权限信息栏
- HTML5 Web 语音 API 在本地不起作用
- HTML5 语音 API - 重置结果数组
- 在 iOS 7+ 的 UIWebView 中使用语音合成 API 设置速率、音高、音量
- Web 语音 API 自定义字词
- 单击加载用户语音API
- 如何更改Chrome'的键盘快捷方式;实现到HTML5语音输入API
- 网络语音API-使用自己的服务
- Web语音API语音合成-获取语音列表
- 为什么我的语音合成 API 语音在函数运行超过 1 次时发生变化
- Angular2: Web Speech API——语音识别