From 038d29ab294e0ffd0a3002aa992308a7d62d33ed Mon Sep 17 00:00:00 2001 From: olivier bouillet Date: Fri, 5 Aug 2022 12:37:35 +0200 Subject: [PATCH] fix: ensure ducking is audio focus is sent always send when speek is called multiple timmes in a row --- .../net/no_mad/tts/TextToSpeechModule.java | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/android/src/main/java/net/no_mad/tts/TextToSpeechModule.java b/android/src/main/java/net/no_mad/tts/TextToSpeechModule.java index 6be44a4..5a7150a 100644 --- a/android/src/main/java/net/no_mad/tts/TextToSpeechModule.java +++ b/android/src/main/java/net/no_mad/tts/TextToSpeechModule.java @@ -3,6 +3,7 @@ import android.media.AudioManager; import android.os.Build; import android.os.Bundle; +import android.util.Log; import android.content.Intent; import android.content.ActivityNotFoundException; import android.app.Activity; @@ -63,6 +64,19 @@ private void setUtteranceProgress() { tts.setOnUtteranceProgressListener(new UtteranceProgressListener() { @Override public void onStart(String utteranceId) { + if (ducking) { + // Request audio focus for playback + int amResult = audioManager.requestAudioFocus(afChangeListener, + // Use the music stream. + AudioManager.STREAM_MUSIC, + // Request permanent focus. + AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK); + + if (amResult != AudioManager.AUDIOFOCUS_REQUEST_GRANTED) { + Log.w("RNTTS", "Android AudioManager error, failed to request audio focus"); + return; + } + } sendEvent("tts-start", utteranceId); } @@ -209,20 +223,6 @@ public void getInitStatus(Promise promise) { public void speak(String utterance, ReadableMap params, Promise promise) { if(notReady(promise)) return; - if(ducking) { - // Request audio focus for playback - int amResult = audioManager.requestAudioFocus(afChangeListener, - // Use the music stream. - AudioManager.STREAM_MUSIC, - // Request permanent focus. - AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK); - - if(amResult != AudioManager.AUDIOFOCUS_REQUEST_GRANTED) { - promise.reject("Android AudioManager error, failed to request audio focus"); - return; - } - } - String utteranceId = Integer.toString(utterance.hashCode()); int speakResult = speak(utterance, utteranceId, params);