问题
I'm trying to get my TextToSpeech
working, but I got a "speak failed: TTS
engine connection not fully set up", but got in green : Connected to ComponentInfo{...GoogleTTSService}.
I didn't found a solution to fix it, and google don't give me interesting things.
Here is my code to have an overview.(you can find the complete code here : See docs
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
public class MainActivity extends Activity implements OnInitListener
{
protected static final int RESULT_SPEECH = 1;
public static final int VOICE_RECOGNITION_REQUEST_CODE = 1234;
public TextToSpeech myTTS;
protected static final int MY_DATA_CHECK_CODE = 0;
@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
int result = myTTS.setLanguage(Locale.getDefault());
if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
Toast.makeText(MainActivity.this,
"This Language is not supported", Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(MainActivity.this,
"Text-To-Speech engine is initialized", Toast.LENGTH_LONG).show();
}
}
else if (status == TextToSpeech.ERROR) {
Toast.makeText(MainActivity.this,
"Error occurred while initializing Text-To-Speech engine", Toast.LENGTH_LONG).show();
}
}
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent checkIntent = new Intent();
checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);
editText = (EditText) findViewById(R.id.editText);
send = (Button)findViewById(R.id.send_button);
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String message = editText.getText().toString();
//add the text in the arrayList
arrayList.add("> " + message);
//sends the message to the server
if (mTcpClient != null) {
mTcpClient.sendMessage(message);
}
//refresh the list
mAdapter.notifyDataSetChanged();
editText.setText("");
}
});
Button btnSpeak = (Button) findViewById(R.id.speak_button);
btnSpeak.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(
RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US");
try {
startActivityForResult(intent, RESULT_SPEECH);
editText.setText("");
} catch (ActivityNotFoundException a) {
Toast t = Toast.makeText(getApplicationContext(),
"Opps! Your device doesn't support Speech to Text",
Toast.LENGTH_SHORT);
t.show();
}
}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case RESULT_SPEECH: {
if (resultCode == RESULT_OK && null != data) {
ArrayList<String> text = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
editText.setText(text.get(0));
send.performClick();
}
break;
}
case MY_DATA_CHECK_CODE: {
if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
// the user has the necessary data - create the TTS
myTTS = new TextToSpeech(this, this);
} else {
// no data - install it now
Intent installTTSIntent = new Intent();
installTTSIntent
.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
startActivity(installTTSIntent);
}
break;
}
}
}
@Override
protected void onDestroy() {
super.onDestroy();
myTTS.shutdown();
}
I'm a very very beginner with Java / Android SDK
... Code could look like very crappy.
If someone can explain me the error, and best of all, give me an answer, it should be very nice.
Thanks, and merry christmas !
回答1:
It seems the method onInit is never executed due to the call to the AsyncTask code. I moved the connect call in the onInit() method instead of the onCreate() and it now works.
Hope it will help someone.
回答2:
The question is old, but I had a similar problem recently. In my case, the first sentence passed to the speak
method was never spoken and LogCat is showing this warning.
I deal with it by placing the TextToSpeech
object as static and created a static
setup
method. Works as a singleton design pattern. I'm calling this method on the first onCreate
of my app. It's working fine 'til now. See below.
public class SpeakerManager {
private static TextToSpeech speaker;
public static void setup() {
if(speaker == null) {
speaker = new TextToSpeech(MyAppUtils.getApplicationContext(), new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS)
speaker.setLanguage(Locale.getDefault());
}
});
}
}
public static void speak(String toBeSpoken) {
speaker.speak(toBeSpoken, TextToSpeech.QUEUE_FLUSH, null, "0000000");
}
public static void pause() {
speaker.stop();
speaker.shutdown();
}
}
This MyAppUtils
comes from this question.
Any improvement will be welcome.
I hope it helps someone.
来源:https://stackoverflow.com/questions/20764214/android-speak-failed-tts-engine-connection-not-fully-set-up