Java 说话失败未绑定到 TTS 引擎
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18129712/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Speak Failed Not Bound to TTS Engine
提问by Man Person
So I had an original activity with basically the same exact code for speaking, but I had to move that code into another activity. The only difference I can tell is that the text to speech is not called in an asynchronous method. The speaking occurs in the speakFull method. I get these errors:
所以我有一个原始活动,它的说话代码基本上完全相同,但我不得不将该代码移动到另一个活动中。我能说的唯一区别是文本到语音不是在异步方法中调用的。说话发生在 speakFull 方法中。我收到这些错误:
speak failed: not bound to TTS engine
isSpeaking failed: not bound to TTS engine
I'm new to android development, I've searched through other solutions to this problem, and I can't really seem to find a solution to make mine work. Any advice, or help is appreciated.
我是 android 开发的新手,我已经搜索了这个问题的其他解决方案,但我似乎无法真正找到解决方案来使我的工作正常进行。任何建议或帮助表示赞赏。
Code:
代码:
package com.example.webview;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.speech.tts.TextToSpeech;
import android.text.method.ScrollingMovementMethod;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class ReadOut extends Activity implements TextToSpeech.OnInitListener, OnClickListener {
boolean paused = false;
String leftToRead = null;
String res = null;
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.read_out);
Intent intent = getIntent();
res = intent.getExtras().getString("response");
TextView textv = (TextView) findViewById(R.id.textView1);
textv.setText(res);
textv.setMovementMethod(new ScrollingMovementMethod());
android.view.Display display = ((android.view.WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
textv.setHeight((int)(display.getHeight()*0.76));
leftToRead = speakFull(res);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
return true;
}
public String speakFull(String text){
System.out.println("Speaking: " + text);
TextToSpeech tts = new TextToSpeech(this, this);
System.out.println("Speaking");
String[] sentences = text.split("\n|\.(?!\d)|(?<!\d)\."); // Regex that splits the body of text into the sentences of that body which are stored in a String array.
for(int i = 0; i < sentences.length; i++){
if(!tts.isSpeaking() && !paused){
System.out.println("Speaking: " + i);
tts.speak(sentences[i], TextToSpeech.QUEUE_FLUSH, null);
}else if(paused){
System.out.println("Paused");
String paused = "";
for(int j = i - 1; j < sentences.length; j++){
paused += sentences[j];
}
return paused;
}else{
i--;
}
if(i == sentences.length - 1){
return "Message 001: Complete";
}
}
return null;
}
@Override
public void onInit(int arg0) {
// TODO Auto-generated method stub
}
public void clickPause(View v){
if(paused){
paused = false;
Button b = (Button) findViewById(R.id.button1);
b.setText("Play");
}else{
paused = true;
Button b = (Button) findViewById(R.id.button1);
b.setText("Pause");
if(leftToRead == null){
leftToRead = speakFull(res);
}else{
leftToRead = speakFull(leftToRead);
}
}
}
@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
}
}
采纳答案by Hoan Nguyen
You can only call speak()
after onInit() was called. So move your tts speak code in onCreate to onInit()
您只能在调用speak()
onInit() 之后调用。因此,将 onCreate 中的 tts speak 代码移动到 onInit()
@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
leftToRead = speakFull(res);
}
and initialize pause to true boolean paused = true;
并将 pause 初始化为 true boolean paused = true;
回答by Boris Karloff
I have this problem but after some minutes. For now I fixed in this way
我有这个问题,但几分钟后。现在我以这种方式固定
myTTS.speak(speech, TextToSpeech.QUEUE_FLUSH, null);
if(!myTTS.isSpeaking()) {
myTTS = new TextToSpeech(this, this);
System.out.println("tts restarted");
}
It is not a good solution, I know, but for now it works. Only 1 problem: at the first fault, the tts doesn't speak.
我知道这不是一个好的解决方案,但现在它有效。只有一个问题:在第一个故障时,tts 不说话。