I am making an app that uses tts to synthesize wav files every time a broadcastreceiver is triggered. Im working on AndroidStudio (latest) and using API level 19 with min 15.
I have a Service with a BroadcastListener. Every time the BroadcastListener onReceive method is executed, I use
tts.synthesizeToFile(playing, null, storagePath + "/" + "tst.wav");
The file is created successfully, but the onDone() method of the UtteranceProgressListener is never called.
public void onCreate() {
super.onCreate();
//Get TTS capabilities
//TODO: Use TextToSpeech.Engine.ACTION_CHECK_TTS_DATA to check if tts is available
tts = new TextToSpeech(PresenterService.this,
new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
//If the TTS engine was started successfully
if (status != TextToSpeech.ERROR) {
tts.setLanguage(Locale.US);
tts.setPitch(PRESENTER_PITCH);
tts.setSpeechRate(PRESENTER_RATE);
}
}
});
tts.setOnUtteranceProgressListener(new TtsUtteranceListener());
IntentFilter filter = new IntentFilter();
filter.addAction("xxxxxxxxxxxxxxxxxx");
mReceiver = new TrackChangedReceiver(tts);
registerReceiver(mReceiver, filter);
}
And the TtsUtteranceListener class:
public class TtsUtteranceListener extends UtteranceProgressListener {
@Override
public void onDone(String utteranceId) {
Log.d("TtsUtteranceListener", "utterance Done: " + utteranceId);
}
@Override
public void onStart(String utteranceId) {
Log.d("TtsUtteranceListener", "utterance Start: " + utteranceId);
}
@Override
public void onError(String utteranceId) {
Log.d("TtsUtteranceListener", "utterance Error: " + utteranceId);
}
}
The method on the listener:
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action != null) {
if (action.equalsIgnoreCase("xxxxxxxxxxxxxxxxx")) {
String playing = "test string";
tts.synthesizeToFile(playing, null, storagePath + "/" + "tst.wav");
}
}
}
I'm using AndroidStudio, and with the debug mode I can see that the mUtteranceProgressListener of the tts object has some object reference:
com.example.android.ttstest.TtsUtteranceListener@41eaf8d8
but the methods of the listener are never called. The Log.d() calls never work and any breakpoints there are never triggered.
I also tried by declaring the UtteranceProgressListener as an anonymous class when calling
tts.setOnUtteranceProgressListener(new UtteranceProgressListener(){...});
but same thing...
Can someone point me in what am I doing wrong?
Ok, I solved it. The problem was that I failed to pass the KEY_PARAM_UTTERANCE_ID
.
if (action.equalsIgnoreCase("xxxxxxxxxxxxxxxxx")) {
HashMap<String, String> hashTts = new HashMap<String, String>();
hashTts.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "id");
String playing = "test string";
tts.synthesizeToFile(playing, hashTts, storagePath + "/" + "tst.wav");
}
I was passing the parameter parameter as null:
tts.synthesizeToFile(playing, null, storagePath + "/" + "tst.wav");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With