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 !