Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ensure ducking is audio focus is sent always send when speek is called multiple times #217

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions android/src/main/java/net/no_mad/tts/TextToSpeechModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
Expand Down