Search code examples
javaandroidandroid-studiotext-to-speechsamsung-galaxy

Android Text to speech plays without error but does not produce any sound


I have an app that plays some text to speech. It works well but I have someone who complains that he can't hear anything on an "Android 10 on a Samsung Galaxy Note 10+ 5G". From the tests I did, it seems that the code is executed without any errors and there is even a pause when the text should be heard, but no sound comes out. Things that were checked:

  1. The TTS engine is google - but it doesn't work on both Google and Samsung.
  2. In the TTS settings when you hit play, you can hear the playback sample.
  3. The language is set to English US

The TTS init code:

private void initTTS() {
    Log.d(TAG, "initTTS: Enter");
    //assume the worst
    mTTSInit = false;

    //create a TTS and do not use it until you get a confirmation that the init process went well
    mTTS = new TextToSpeech(this, status -> {

        //OnInit of TTS is run on the main thread and so is VERY slow
        new Thread(() -> {
            if (status == TextToSpeech.SUCCESS) {
                //use English - not sure about other languages at the moment.
                mTTS.setSpeechRate(0.7f);
                mTTS.setPitch(1.1f);
                int result = mTTS.setLanguage(Locale.US);

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

                    //notify the user that TTS will not work on this device
                    showToastOnUIThread(getResources().getString(R.string.TTS_missing_lang_error));
                } else {
                    Log.d(TAG, "onInit: SUCCESS");
                    //Init went fine.
                    //Set a listener when the TTS message finish as we sometime want
                    //to chime if a tile with a gem was produced.
                    mTTS.setOnUtteranceProgressListener(new UtteranceProgressListener() {
                        @Override
                        public void onStart(String utteranceId) {
                            showToastOnUIThread("About to play message - raising volume");
                            AudioManager am = (AudioManager) getSystemService(getApplicationContext().AUDIO_SERVICE);
                            am.setStreamVolume(AudioManager.STREAM_MUSIC,
                                    am.getStreamMaxVolume(AudioManager.STREAM_MUSIC),
                                    0);
                        }

                        @Override
                        public void onDone(String utteranceId) {
                            Log.d(TAG, "onDone: TTS: " + utteranceId);
                            showToastOnUIThread("Message was: " + utteranceId);
                            playChimeSound();
                        }

                        @Override
                        public void onError(String utteranceId) {
                            Log.d(TAG, "onError: TTS error while trying to say: " + utteranceId);
                        }
                    });
                    mTTSInit = true;
                    runOnUiThread(() -> mTTSStatusButton.setVisibility(View.GONE));
                }
            } else {
                Log.e("TTS", "Initialization failed");
                //notify the user that TTS will not work on this device
                showToastOnUIThread(getResources().getString(R.string.TTS_missing_lang_error));
            }
        }).start();
    });
}

The TTS playback code:

if (readOutLoud && mTTSInit) {
        String text = getString(R.string.tile) + " " + mViewModel.getLastSelectedTile();

        //to get a call back from TTS we mst supply a KEY_PARAM_UTTERANCE_ID
        HashMap<String, String> ttsHashMap = new HashMap<>();
        ttsHashMap.put(TextToSpeech.Engine.KEY_PARAM_STREAM, String.valueOf(AudioManager.STREAM_NOTIFICATION));
        ttsHashMap.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, text);
        ttsHashMap.put(TextToSpeech.Engine.KEY_PARAM_VOLUME, "1");
        mTTS.speak(text, TextToSpeech.QUEUE_FLUSH, ttsHashMap);
    }

Solution

  • Try removing:

    ttsHashMap.put(TextToSpeech.Engine.KEY_PARAM_STREAM, String.valueOf(AudioManager.STREAM_NOTIFICATION));