Search code examples
javaandroidtext-to-speech

TextToSpeech android API 22 not playing audio


I am using text to speech engine in my App. It works fine on emulator Nexus 6 with API 23 and higher. But on emulator Nexus 6 with API 22 it does not speak. Both emulators use Pico TTS as preferred engine.

My activity layout contains only one button "Speak". This is my activity code:

public class MainActivity extends AppCompatActivity {
private TextToSpeech mTTS;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button speakBtn = findViewById(R.id.button);
        speakBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                speak();
            }
        });

        mTTS = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {
                if (status == TextToSpeech.SUCCESS) {
                    int result = mTTS.setLanguage(Locale.US);

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

                    mTTS.setOnUtteranceProgressListener(new UtteranceProgressListener() {
                    @Override
                    public void onStart(String utteranceId) {
                        Log.d("TTS", "onStart called, utteranceId = " + utteranceId);
                    }

                    @Override
                    public void onDone(String utteranceId) {
                        Log.d("TTS", "onDone called, utteranceId = " + utteranceId);
                    }

                    @Override
                    public void onError(String utteranceId) {
                        Log.d("TTS", "onError called, utteranceId = " + utteranceId);
                    }
                });

                } else {
                    Log.e("TTS", "Initialization failed");
                }
            }
        });
    }

    private void speak() {
       Log.d("TTS", "speak() method called");
       HashMap<String, String> map = new HashMap<>();
       map.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "greeting");

       mTTS.speak("hello", TextToSpeech.QUEUE_FLUSH, map);
    }
}

This is all logs from emulator Nexus 6 API 22:

02-18 13:54:09.942 9739-9739/? E/libprocessgroup: failed to make and chown /acct/uid_10059: Read-only file system

02-18 13:54:09.943 9739-9739/? W/Zygote: createProcessGroup failed, kernel missing CONFIG_CGROUP_CPUACCT?

02-18 13:54:09.943 9739-9739/? I/art: Not late-enabling -Xcheck:jni (already on)

02-18 13:54:09.961 9739-9748/? E/art: Failed sending reply to debugger: Broken pipe

02-18 13:54:09.961 9739-9748/? I/art: Debugger is no longer active

02-18 13:54:09.992 9739-9739/? W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable

02-18 13:54:10.001 9739-9739/? I/art: Rejecting re-init on previously-failed class java.lang.Class

02-18 13:54:10.001 9739-9739/? I/art: Rejecting re-init on previously-failed class java.lang.Class

02-18 13:54:10.064 9739-9739/? I/TextToSpeech: Sucessfully bound to com.svox.pico

02-18 13:54:10.072 9739-9757/? D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true

02-18 13:54:10.074 9739-9739/? D/Atlas: Validating map...

02-18 13:54:10.093 9739-9739/? I/TextToSpeech: Connected to ComponentInfo{com.svox.pico/com.svox.pico.PicoService}

02-18 13:54:10.096 9739-9758/? I/TextToSpeech: Set up connection to ComponentInfo{com.svox.pico/com.svox.pico.PicoService}

02-18 13:54:10.111 9739-9757/? I/OpenGLRenderer: Initialized EGL, version 1.4

02-18 13:54:10.111 9739-9757/? W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...

02-18 13:54:10.128 9739-9757/? D/EGL_emulation: eglCreateContext: 0xae434e20: maj 2 min 0 rcv 2

02-18 13:54:10.131 9739-9757/? D/EGL_emulation: eglMakeCurrent: 0xae434e20: ver 2 0

02-18 13:54:10.134 9739-9757/? D/OpenGLRenderer: Enabling debug mode 0

02-18 13:54:10.174 9739-9757/? D/EGL_emulation: eglMakeCurrent: 0xae434e20: ver 2 0

02-19 09:32:25.570 9739-9739/com.example.ttsapp D/TTS: speak() method called


Solution

  • Figured out how to solve the problem. Everything works if launch the emulator not via Debugging or Execution but from AVD Manager after starting Android Studio.