From ebd589c9cbe7c73f4a2ba174438b533529860668 Mon Sep 17 00:00:00 2001 From: TacoTheDank Date: Wed, 30 Dec 2020 17:10:57 -0500 Subject: [PATCH] Use AndroidX Media compat in AudioReactor --- app/build.gradle | 1 + .../newpipe/player/helper/AudioReactor.java | 39 ++++++------------- 2 files changed, 13 insertions(+), 27 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 15561b2d21a..15b0668c6e7 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -203,6 +203,7 @@ dependencies { implementation 'androidx.core:core-ktx:1.3.2' implementation 'androidx.documentfile:documentfile:1.0.1' implementation 'androidx.localbroadcastmanager:localbroadcastmanager:1.0.0' + implementation 'androidx.media:media:1.2.1' implementation 'androidx.webkit:webkit:1.4.0' implementation "androidx.lifecycle:lifecycle-livedata:${androidxLifecycleVersion}" diff --git a/app/src/main/java/org/schabi/newpipe/player/helper/AudioReactor.java b/app/src/main/java/org/schabi/newpipe/player/helper/AudioReactor.java index ffe19599d20..13ee24e16cf 100644 --- a/app/src/main/java/org/schabi/newpipe/player/helper/AudioReactor.java +++ b/app/src/main/java/org/schabi/newpipe/player/helper/AudioReactor.java @@ -5,14 +5,14 @@ import android.animation.ValueAnimator; import android.content.Context; import android.content.Intent; -import android.media.AudioFocusRequest; import android.media.AudioManager; import android.media.audiofx.AudioEffect; -import android.os.Build; import android.util.Log; import androidx.annotation.NonNull; import androidx.core.content.ContextCompat; +import androidx.media.AudioFocusRequestCompat; +import androidx.media.AudioManagerCompat; import com.google.android.exoplayer2.SimpleExoPlayer; import com.google.android.exoplayer2.analytics.AnalyticsListener; @@ -21,20 +21,17 @@ public class AudioReactor implements AudioManager.OnAudioFocusChangeListener, An private static final String TAG = "AudioFocusReactor"; - private static final boolean SHOULD_BUILD_FOCUS_REQUEST = - Build.VERSION.SDK_INT >= Build.VERSION_CODES.O; - private static final int DUCK_DURATION = 1500; private static final float DUCK_AUDIO_TO = .2f; - private static final int FOCUS_GAIN_TYPE = AudioManager.AUDIOFOCUS_GAIN; + private static final int FOCUS_GAIN_TYPE = AudioManagerCompat.AUDIOFOCUS_GAIN; private static final int STREAM_TYPE = AudioManager.STREAM_MUSIC; private final SimpleExoPlayer player; private final Context context; private final AudioManager audioManager; - private final AudioFocusRequest request; + private final AudioFocusRequestCompat request; public AudioReactor(@NonNull final Context context, @NonNull final SimpleExoPlayer player) { @@ -43,15 +40,11 @@ public AudioReactor(@NonNull final Context context, this.audioManager = ContextCompat.getSystemService(context, AudioManager.class); player.addAnalyticsListener(this); - if (SHOULD_BUILD_FOCUS_REQUEST) { - request = new AudioFocusRequest.Builder(FOCUS_GAIN_TYPE) - .setAcceptsDelayedFocusGain(true) - .setWillPauseWhenDucked(true) - .setOnAudioFocusChangeListener(this) - .build(); - } else { - request = null; - } + request = new AudioFocusRequestCompat.Builder(FOCUS_GAIN_TYPE) + //.setAcceptsDelayedFocusGain(true) + .setWillPauseWhenDucked(true) + .setOnAudioFocusChangeListener(this) + .build(); } public void dispose() { @@ -64,19 +57,11 @@ public void dispose() { //////////////////////////////////////////////////////////////////////////*/ public void requestAudioFocus() { - if (SHOULD_BUILD_FOCUS_REQUEST) { - audioManager.requestAudioFocus(request); - } else { - audioManager.requestAudioFocus(this, STREAM_TYPE, FOCUS_GAIN_TYPE); - } + AudioManagerCompat.requestAudioFocus(audioManager, request); } public void abandonAudioFocus() { - if (SHOULD_BUILD_FOCUS_REQUEST) { - audioManager.abandonAudioFocusRequest(request); - } else { - audioManager.abandonAudioFocus(this); - } + AudioManagerCompat.abandonAudioFocusRequest(audioManager, request); } public int getVolume() { @@ -88,7 +73,7 @@ public void setVolume(final int volume) { } public int getMaxVolume() { - return audioManager.getStreamMaxVolume(STREAM_TYPE); + return AudioManagerCompat.getStreamMaxVolume(audioManager, STREAM_TYPE); } /*//////////////////////////////////////////////////////////////////////////