Varun Ajay Gupta
Varun Ajay Gupta

Reputation: 349

Text to speech class showing error (android)

Hi i am a newbie in android development ,i am creating a sms and call reader application in which i have used a service in which i have used Text to speech class. And i am getting an error speak failed: not bound to TTS Engine. Can anyone help ? Thanks

Texttospeech.java

package com.example.sms_reader;

import java.util.Locale;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.speech.tts.TextToSpeech;
import android.util.Log;

public class Myservice extends Service{
     TextToSpeech tts;
     String   content2;
     public void onCreate() {       
        super.onCreate();
        tts = new TextToSpeech(this, null);

     }               
     public int onStartCommand(Intent intent, int flags, int startId) {
            //TODO do something useful        
             Log.d("tag4", "INTENT RECEIVED");
             content2 = intent.getStringExtra("content");       
             Log.d("tag6", content2);
             speakOut(); 

         return Service.START_NOT_STICKY;

     }


private void speakOut() {


    tts.speak(content2, TextToSpeech.QUEUE_FLUSH, null);
}   

@Override
public void onDestroy() {
    // Don't forget to shutdown!
    if (tts != null) {
        tts.stop();
        tts.shutdown();
    }
    super.onDestroy();
}

public void onInit(int status) {
    // TODO Auto-generated method stub

    if (status == TextToSpeech.SUCCESS) {

        int result = tts.setLanguage(Locale.US);

        // tts.setPitch(5); // set pitch level

        // tts.setSpeechRate(2); // set speech speed rate

        if (result == TextToSpeech.LANG_MISSING_DATA
                || result == TextToSpeech.LANG_NOT_SUPPORTED) {
            Log.e("TTS", "Language is not supported");
        } 
        else {

            speakOut();
        }

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

}

    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }

}

Upvotes: 0

Views: 119

Answers (1)

Ajit
Ajit

Reputation: 724

Replace tts = new TextToSpeech(this, null); by tts = new TextToSpeech(this, this);

Upvotes: 2

Related Questions