mirror of
https://codeberg.org/comaps/comaps
synced 2025-12-19 21:13:35 +00:00
[android] Simplify TTS code using AudioManagerCompat
Signed-off-by: Isira Seneviratne <31027858+Isira-Seneviratne@users.noreply.github.com>
This commit is contained in:
@@ -20,6 +20,7 @@ androidx-preference = { module = "androidx.preference:preference", version = "1.
|
|||||||
androidx-recyclerview = { module = "androidx.recyclerview:recyclerview", version = "1.4.0" }
|
androidx-recyclerview = { module = "androidx.recyclerview:recyclerview", version = "1.4.0" }
|
||||||
androidx-work-runtime = { module = "androidx.work:work-runtime", version = "2.10.5" }
|
androidx-work-runtime = { module = "androidx.work:work-runtime", version = "2.10.5" }
|
||||||
androidx-lifecycle-process = { module = "androidx.lifecycle:lifecycle-process", version = "2.9.4" }
|
androidx-lifecycle-process = { module = "androidx.lifecycle:lifecycle-process", version = "2.9.4" }
|
||||||
|
androidx-media = { module = "androidx.media:media", version = "1.7.1" }
|
||||||
android-material = { module = "com.google.android.material:material", version = "1.12.0" }
|
android-material = { module = "com.google.android.material:material", version = "1.12.0" }
|
||||||
google-guava = { module = "com.google.guava:guava", version = "33.4.8-android" }
|
google-guava = { module = "com.google.guava:guava", version = "33.4.8-android" }
|
||||||
appdevnext-androidchart = { module = "com.github.AppDevNext:AndroidChart", version = "3.1.0.31" }
|
appdevnext-androidchart = { module = "com.github.AppDevNext:AndroidChart", version = "3.1.0.31" }
|
||||||
|
|||||||
@@ -122,6 +122,7 @@ dependencies {
|
|||||||
implementation libs.androidx.annotation
|
implementation libs.androidx.annotation
|
||||||
implementation libs.androidx.fragment
|
implementation libs.androidx.fragment
|
||||||
implementation libs.androidx.lifecycle.process
|
implementation libs.androidx.lifecycle.process
|
||||||
|
implementation libs.androidx.media
|
||||||
implementation libs.androidx.recyclerview
|
implementation libs.androidx.recyclerview
|
||||||
implementation libs.android.material
|
implementation libs.android.material
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package app.organicmaps.sdk.sound;
|
|||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.database.ContentObserver;
|
import android.database.ContentObserver;
|
||||||
|
import android.media.AudioManager;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
import android.os.Looper;
|
import android.os.Looper;
|
||||||
@@ -12,6 +13,10 @@ import android.text.TextUtils;
|
|||||||
import android.util.Pair;
|
import android.util.Pair;
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
import androidx.annotation.Nullable;
|
import androidx.annotation.Nullable;
|
||||||
|
import androidx.core.content.ContextCompat;
|
||||||
|
import androidx.media.AudioAttributesCompat;
|
||||||
|
import androidx.media.AudioFocusRequestCompat;
|
||||||
|
import androidx.media.AudioManagerCompat;
|
||||||
import app.organicmaps.sdk.util.Config;
|
import app.organicmaps.sdk.util.Config;
|
||||||
import app.organicmaps.sdk.util.concurrency.UiThread;
|
import app.organicmaps.sdk.util.concurrency.UiThread;
|
||||||
import app.organicmaps.sdk.util.log.Logger;
|
import app.organicmaps.sdk.util.log.Logger;
|
||||||
@@ -51,7 +56,16 @@ public enum TtsPlayer
|
|||||||
private TextToSpeech mTts;
|
private TextToSpeech mTts;
|
||||||
private boolean mInitializing;
|
private boolean mInitializing;
|
||||||
private boolean mReloadTriggered = false;
|
private boolean mReloadTriggered = false;
|
||||||
private AudioFocusManager mAudioFocusManager;
|
|
||||||
|
private final AudioFocusRequestCompat mAudioFocusRequest =
|
||||||
|
new AudioFocusRequestCompat.Builder(AudioManagerCompat.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK)
|
||||||
|
.setAudioAttributes(new AudioAttributesCompat.Builder()
|
||||||
|
.setUsage(AudioAttributesCompat.USAGE_ASSISTANCE_NAVIGATION_GUIDANCE)
|
||||||
|
.setContentType(AudioAttributesCompat.CONTENT_TYPE_SPEECH)
|
||||||
|
.build())
|
||||||
|
.setOnAudioFocusChangeListener(focusChange -> {})
|
||||||
|
.build();
|
||||||
|
private AudioManager mAudioManager;
|
||||||
|
|
||||||
private final Bundle mParams = new Bundle();
|
private final Bundle mParams = new Bundle();
|
||||||
|
|
||||||
@@ -167,29 +181,29 @@ public enum TtsPlayer
|
|||||||
@Override
|
@Override
|
||||||
public void onStart(String utteranceId)
|
public void onStart(String utteranceId)
|
||||||
{
|
{
|
||||||
mAudioFocusManager.requestAudioFocus();
|
AudioManagerCompat.requestAudioFocus(mAudioManager, mAudioFocusRequest);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onDone(String utteranceId)
|
public void onDone(String utteranceId)
|
||||||
{
|
{
|
||||||
mAudioFocusManager.releaseAudioFocus();
|
AudioManagerCompat.abandonAudioFocusRequest(mAudioManager, mAudioFocusRequest);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@SuppressWarnings("deprecated") // abstract method must be implemented
|
@SuppressWarnings("deprecated") // abstract method must be implemented
|
||||||
public void onError(String utteranceId)
|
public void onError(String utteranceId)
|
||||||
{
|
{
|
||||||
mAudioFocusManager.releaseAudioFocus();
|
AudioManagerCompat.abandonAudioFocusRequest(mAudioManager, mAudioFocusRequest);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onError(String utteranceId, int errorCode)
|
public void onError(String utteranceId, int errorCode)
|
||||||
{
|
{
|
||||||
mAudioFocusManager.releaseAudioFocus();
|
AudioManagerCompat.abandonAudioFocusRequest(mAudioManager, mAudioFocusRequest);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
mAudioFocusManager = new AudioFocusManager(context);
|
mAudioManager = ContextCompat.getSystemService(context, AudioManager.class);
|
||||||
mParams.putFloat(TextToSpeech.Engine.KEY_PARAM_VOLUME, Config.TTS.getVolume());
|
mParams.putFloat(TextToSpeech.Engine.KEY_PARAM_VOLUME, Config.TTS.getVolume());
|
||||||
mInitializing = false;
|
mInitializing = false;
|
||||||
if (mReloadTriggered && sOnReloadCallback != null)
|
if (mReloadTriggered && sOnReloadCallback != null)
|
||||||
@@ -230,12 +244,10 @@ public enum TtsPlayer
|
|||||||
if (Config.TTS.isEnabled())
|
if (Config.TTS.isEnabled())
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
boolean isMusicActive = mAudioFocusManager.requestAudioFocus();
|
final boolean isMusicActive = mAudioManager.isMusicActive();
|
||||||
if (isMusicActive)
|
AudioManagerCompat.requestAudioFocus(mAudioManager, mAudioFocusRequest);
|
||||||
delayHandler.postDelayed(
|
final long delay = isMusicActive ? TTS_SPEAK_DELAY_MILLIS : 0;
|
||||||
() -> mTts.speak(textToSpeak, TextToSpeech.QUEUE_ADD, mParams, textToSpeak), TTS_SPEAK_DELAY_MILLIS);
|
delayHandler.postDelayed(() -> mTts.speak(textToSpeak, TextToSpeech.QUEUE_ADD, mParams, textToSpeak), delay);
|
||||||
else
|
|
||||||
mTts.speak(textToSpeak, TextToSpeech.QUEUE_ADD, mParams, textToSpeak);
|
|
||||||
}
|
}
|
||||||
catch (IllegalArgumentException e)
|
catch (IllegalArgumentException e)
|
||||||
{
|
{
|
||||||
@@ -255,7 +267,7 @@ public enum TtsPlayer
|
|||||||
if (isReady())
|
if (isReady())
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
mAudioFocusManager.releaseAudioFocus();
|
AudioManagerCompat.abandonAudioFocusRequest(mAudioManager, mAudioFocusRequest);
|
||||||
mTts.stop();
|
mTts.stop();
|
||||||
}
|
}
|
||||||
catch (IllegalArgumentException e)
|
catch (IllegalArgumentException e)
|
||||||
|
|||||||
Reference in New Issue
Block a user