ak1394 / react-native-tts

React Native Text-To-Speech library for Android and iOS
601 stars 154 forks source link

Tts.speak(word) crash on android 4.x #121

Open duyv opened 5 years ago

duyv commented 5 years ago

It's working fine in android 5.x and above but crash in android 4.x. I was use android studio logcat and this show me the error log. I think it maybe not support for android 4.x.

07-01 15:31:51.165 2248-2265/com.svox.pico E/AndroidRuntime: FATAL EXCEPTION: TTS.AudioPlaybackThread
    Process: com.svox.pico, PID: 2248
    java.lang.IllegalArgumentException: Invalid stream type.
        at android.media.AudioTrack.audioParamCheck(AudioTrack.java:373)
        at android.media.AudioTrack.<init>(AudioTrack.java:315)
        at android.media.AudioTrack.<init>(AudioTrack.java:265)
        at android.speech.tts.BlockingAudioTrack.createStreamingAudioTrack(BlockingAudioTrack.java:218)
        at android.speech.tts.BlockingAudioTrack.init(BlockingAudioTrack.java:96)
        at android.speech.tts.SynthesisPlaybackQueueItem.run(SynthesisPlaybackQueueItem.java:90)
        at android.speech.tts.AudioPlaybackHandler$MessageLoop.run(AudioPlaybackHandler.java:134)
        at java.lang.Thread.run(Thread.java:841)
felipecespedes commented 4 years ago

I was having this same error, so it seems that in order to support Android 4.x you should pass the KEY_PARAM_STREAM value explicitly to the speak() method

Tts.speak(text, { androidParams: { KEY_PARAM_STREAM: 'STREAM_MUSIC' } });

You can see all the supported values for KEY_PARAM_STREAM on the README file https://github.com/ak1394/react-native-tts#speaking

Hope it works.