Skip to content

Commit

Permalink
Merge pull request #5065 from TacoTheDank/androidx-media-audioreactor
Browse files Browse the repository at this point in the history
Use AndroidX Media compat in AudioReactor
  • Loading branch information
Redirion authored Dec 31, 2020
2 parents 8fe1a76 + ebd589c commit 8193a0d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 27 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand All @@ -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() {
Expand All @@ -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() {
Expand All @@ -88,7 +73,7 @@ public void setVolume(final int volume) {
}

public int getMaxVolume() {
return audioManager.getStreamMaxVolume(STREAM_TYPE);
return AudioManagerCompat.getStreamMaxVolume(audioManager, STREAM_TYPE);
}

/*//////////////////////////////////////////////////////////////////////////
Expand Down

0 comments on commit 8193a0d

Please sign in to comment.