Search code examples
androidnotificationstext-to-speech

TTS Received But No Voice Coming Out


I am trying to use TextToSpeech but i have some strange problems.

Let me explain my application's architecture simply. This application will be used for bus hours. Users can add their buses to notification. Server will notify subscribers if anything changes. I want to use TTS with this application.

I implemented Android GCM and notification system, only problem is TTS.

GCMIntentService.onMessage method calls generateNotification method below:

public static void generateNotification(Context context, String message) {
        int icon = R.drawable.icon_small;
        long when = System.currentTimeMillis();
        NotificationManager notificationManager = (NotificationManager)
                context.getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = new Notification(icon, message, when);
        String title = context.getString(R.string.app_name);       

        Bundle b = new Bundle();
        b.putString(EXTRA_MESSAGE, message);
        b.putString(READABLE_MESSAGE, message);

        Intent notificationIntent = new Intent(context, BusAlerts.class);
        notificationIntent.putExtra("CallType", CallType.NOTIFICATION);
        notificationIntent.putExtra("MessageReceived", b);
        // set intent so it does not start a new activity
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
                Intent.FLAG_ACTIVITY_NEW_TASK);
        PendingIntent intent =
                PendingIntent.getActivity(context, 0, notificationIntent,  Intent.FLAG_ACTIVITY_NEW_TASK);
        notification.setLatestEventInfo(context, title, message, intent);
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        notification.defaults |= Notification.DEFAULT_VIBRATE | Notification.DEFAULT_SOUND;
        long[] vibrate = { 1000 };
        notification.vibrate = vibrate;          
        notificationManager.notify(0, notification);
    }

This code successfully generates notification. Here is BusAlerts.codes (some trimmed ;))

public class BusAlerts extends Activity implements OnInitListener {
private static TextToSpeech myTts;

@Override
    public void onCreate(Bundle savedInstanceState) {
        myTts = new TextToSpeech(this, null);
}

@Override
protected void onStart() {
        super.onStart();

//readableMessage extracts from bundles

speak(readableMessage);
}

public void speak(String text)
    {
         myTts.speak(text, TextToSpeech.QUEUE_FLUSH, null);     
    }
@Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
         if (myTts != null) {
             myTts.shutdown();
             myTts.stop();
             myTts = null;
            }
    }
public void onInit(int status) {
         if (status == TextToSpeech.SUCCESS) {

                int result = myTts.setLanguage(Locale.UK);

                if (result == TextToSpeech.LANG_MISSING_DATA
                        || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                    Log.e("TTS", "This Language is not supported");
                } 
                else{
                    if (myTts.isLanguageAvailable(Locale.UK) == TextToSpeech.LANG_AVAILABLE || myTts.isLanguageAvailable(Locale.UK) == TextToSpeech.LANG_COUNTRY_AVAILABLE)
                        myTts.setLanguage(Locale.UK);
                }

            } else {
                Log.e("TTS", "Initilization Failed!");
            }

    }

If users open this class from notification, everything works perfect but no sound coming out. When i debug it i see a situation as below. enter image description here

But if user presses Home button and reopens application (same activity resumes) user can hear sound. Its situation is like this. enter image description here

As i noticed mCachedParamters and mITts differs; but i don't know why. I have been stucked it for a week and couldn't find solution.


Solution

  • You should call

    speak(readableMessage);
    

    only after OnInit has been called. By calling this in Onstart you are not guaranteed of this. That is the reason when you come back to application it is initialized and you are able to hear