22

Can someone help me using TTS with API 21. All examples available are deprecated with version 21

Here's my code giving error on last line:

Calendar cal = Calendar.getInstance();
                    cal.getTime();
                    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
                    String text = sdf.toString();
                    btn.setText("Ouvir as Horas");

                    TextToSpeech tts = new TextToSpeech(NightClock.this,(TextToSpeech.OnInitListener) NightClock.this);
                    tts.setLanguage(Locale.US);
                    tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);

In Android developers it says that this method is deprecated and replaced by this:

speak(String text, int queueMode, HashMap params) This method was deprecated in API level 21. As of API level 21, replaced by speak(CharSequence, int, Bundle, String).

Can someone help to code my app.

Chin
  • 19,717
  • 37
  • 107
  • 164
Jose Borges
  • 340
  • 2
  • 4
  • 14

6 Answers6

44

I searched various site. Finally, I think I could get the answer for your Question...

Instead calling tts.speak() directly, put the following if-else statement.

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    ttsGreater21(text);
} else {
    ttsUnder20(text);
}

Then declare ttsGreater21() and ttsUnder20() as follows.

@SuppressWarnings("deprecation")
private void ttsUnder20(String text) {
    HashMap<String, String> map = new HashMap<>();
    map.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "MessageId");
    tts.speak(text, TextToSpeech.QUEUE_FLUSH, map);
}

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void ttsGreater21(String text) {
    String utteranceId=this.hashCode() + "";
    tts.speak(text, TextToSpeech.QUEUE_FLUSH, null, utteranceId);
}

I confirmed above code with Genymotion VM Android 5.0 and Android 4.4.4.

xanadu6291
  • 931
  • 10
  • 20
7
tts.speak(text,TextToSpeech.QUEUE_FLUSH,null,null);

Try this.

Pang
  • 9,564
  • 146
  • 81
  • 122
Aditya
  • 371
  • 3
  • 8
5

So I guess this is the trick:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    tts.speak("12 e8", TextToSpeech.QUEUE_FLUSH, null, null);
    }
else {
    tts.speak("12 e8", TextToSpeech.QUEUE_FLUSH, null);
    }

I just need to test this on emulator.

By the way, @Aditya since you've been so helpful I've been stuck in the same project where it should speak that TextToSpeech and turns on the screen but I haven't manage to turn the screen on. I have tried to use wakelocks and flags from all the examples I've found :) This is done trough the proximity sensor that I managed to work. It says the text but doesn't show the screen. Can you help me on this?

Well practice is the key of success. All the suggested answer by me are perfectly working in my eclipse IDE. Solution of your screen lock is below

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
 PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag");
 wl.acquire();
   ..screen will stay on during this section..
 wl.release();
Ergest Basha
  • 7,870
  • 4
  • 8
  • 28
Jose Borges
  • 340
  • 2
  • 4
  • 14
  • 6
    Instead of your current condition, you should use `if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { ... } else { ... }` – EpicPandaForce Jan 20 '15 at 13:09
2

(1) My activity implements TextToSpeech.OnInitListener

(2) I play my synthesized speech in the onInit method, but I suppose (not tried it) you could play it anytime after onInit() is called. But this is the key, you have to wait for the TextToSpeech engine to initialize.

public class MainActivity extends AppCompatActivity implements TextToSpeech.OnInitListener {
    public TextToSpeech mTTS;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTTS = new TextToSpeech(this, this);
    }

    @Override
    public void onInit(int i) {
        mTTS.setLanguage(Locale.UK);
        mTTS.speak("Hello, how are you?", TextToSpeech.QUEUE_ADD, null, null);
    }    
}
unshul
  • 269
  • 3
  • 16
0

Try this

tts=new TextToSpeech(getBaseContext(),new TextToSpeech.OnInitListener() 
{       
       @Override        
       public void onInit(int status) 
       {
            tts.setLanguage(Locale.getDefault());
            tts.setPitch(1.3f);
            tts.setSpeechRate(1f);
       }
});
EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
Aditya
  • 371
  • 3
  • 8
  • it's a bit confusing for me. I tried several ways but all give errors. For the line that it's giving error this: "speak(CharSequence text, int queueMode, Bundle params, String utteranceId)" .It's what Android Studio suggests. I'm a newbie I don't know how can I fill those fields. – Jose Borges Jan 15 '15 at 18:19
  • Initialize Text to Speech on top as in my answer – Aditya Jan 16 '15 at 07:06
  • tts.speak(tv3.getText().toString(),TextToSpeech.QUEUE_FLUSH,null); tv is text view or you can use a line as – Aditya Jan 16 '15 at 07:06
  • tts.speak("Hello its TTS",TextToSpeech.QUEUE_FLUSH,null); – Aditya Jan 16 '15 at 07:07
  • That method is deprectaed with API 21, that's my problem, it's this syntax speak(String text, int queueMode, HashMap params), The one suggested by Android Studio is:speak(CharSequence text, int queueMode, Bundle params, String utteranceId). – Jose Borges Jan 16 '15 at 13:02
0

The wakelock, I managed to make ti work this way:

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP |
                                    PowerManager.ON_AFTER_RELEASE, "MyWakelock");

wl.aquire();
wl.release();
Jose Borges
  • 340
  • 2
  • 4
  • 14