Home Ask Login Register

Developers Planet

Your answer is one click away!

Amulya D V February 2016

Speak failed: tts engine connection not fully setup in the TextToSpeech functionality

package com.example.trynot;

import java.io.IOException;
import java.util.Calendar;
import java.util.Locale;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Flags;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.NoSuchProviderException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.Flags.Flag;
import javax.mail.search.FlagTerm;

import com.example.trynot.MainActivity;
import com.example.trynot.R;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.sax.StartElementListener;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationCompat.Builder;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import android.speech.tts.TextToSpeech;

public class MainActivity extends Activity {
    private static final int MY_DATA_CHECK_CODE = 1234;
    public static Context c,b;
    public TextToSpeech tts;
    public static Intent serviceIntent;
    private static int myNotificationId;

    public static class  ReadMailSample extends AsyncTask<String,String,Void> implements TextToSpeech.OnInitListener{
        Message message;
        private TextToSpeech tts;
        static String command,         


OYRM February 2016

From the Api Docs we see that your implementation of OnInitListener is necessesary to receive a notification that the TTS initialization is complete. This is described in the TextToSpeech docs as well where it is stated that the speech will fail until initialized. In the following, I will not attempt to fix all of your code so that it works, but I will point out how you're mis-using the TTS framework currently and nudge you in the right direction.

Though you have implemented onInitListener, you aren't doing anything useful with that implementation. Below, I show something more useful in which one captures the result of onInitListener's onInit callback, in order to remain aware of the initialization state of TTS.

private boolean mIsTTSInited = false; // member indicating TTS initialization state
// Receives notification of initialization
public void onInit(int status) {
    if (status == TextToSpeech.SUCCESS) {
        Log.i("TTS", "Initilization Succeeded!");
        // save the state indicating success of TTS initialization
        mIsTTSInited = true;
    } else {
        Log.e("TTS", "Initilization Failed!");
        mIsTTSInited = false;
protected void onProgressUpdate(String... values) {
    // A trivial example to show how you can check the state of initialization for TTS
    if (mIsTTSInited) {
    } else {
        // code to deal with TTS not init'ed case

Additionally, I wouldn't run the TextToSpeech initialization as part of the AsyncTask. If I were you, I'd move

tts = new TextToSpeech(c,ReadMailSample.this);


Post Status

Asked in February 2016
Viewed 1,280 times
Voted 6
Answered 1 times


Leave an answer

Quote of the day: live life