UtteranceProgressListener not working for Android TTS

I am creating an application that uses tts to synthesize wav files every time the translation starts. Im runs on AndroidStudio (the latter) and uses 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 was created successfully, but the onDone () method of the UtteranceProgressListener object 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);
    }
}

Listener method:

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 use AndroidStudio, and in debug mode, I see that the tts object's mUtteranceProgressListener has some object reference:

com.example.android.ttstest.TtsUtteranceListener@41eaf8d8

but listener methods are never called. Log.d () calls never work, and any breakpoints never start.

UtteranceProgressListener

tts.setOnUtteranceProgressListener(new UtteranceProgressListener(){...});

...

- , ?

+4
2

, . , 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");
}

null:

tts.synthesizeToFile(playing, null, storagePath + "/" + "tst.wav");
+7

, . , .

1. . ( , )

class Waiter extends AsyncTask<Void,Void,Void>{
        @Override
        protected Void doInBackground(Void... voids) {
            while (tts.isSpeaking()){
                try{Thread.sleep(1000);}catch (Exception e){}
            }
            return null;
        }
        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            //TTS has finished speaking. WRITE YOUR CODE HERE
        }
    }

2: tts.speak(...)

tts.speak(...);
new Waiter().execute();
0

Source: https://habr.com/ru/post/1527394/


All Articles