Use AndroidX Media compat in AudioReactor
This commit is contained in:
parent
28802805f8
commit
ebd589c9cb
|
@ -203,6 +203,7 @@ dependencies {
|
||||||
implementation 'androidx.core:core-ktx:1.3.2'
|
implementation 'androidx.core:core-ktx:1.3.2'
|
||||||
implementation 'androidx.documentfile:documentfile:1.0.1'
|
implementation 'androidx.documentfile:documentfile:1.0.1'
|
||||||
implementation 'androidx.localbroadcastmanager:localbroadcastmanager:1.0.0'
|
implementation 'androidx.localbroadcastmanager:localbroadcastmanager:1.0.0'
|
||||||
|
implementation 'androidx.media:media:1.2.1'
|
||||||
implementation 'androidx.webkit:webkit:1.4.0'
|
implementation 'androidx.webkit:webkit:1.4.0'
|
||||||
|
|
||||||
implementation "androidx.lifecycle:lifecycle-livedata:${androidxLifecycleVersion}"
|
implementation "androidx.lifecycle:lifecycle-livedata:${androidxLifecycleVersion}"
|
||||||
|
|
|
@ -5,14 +5,14 @@ import android.animation.AnimatorListenerAdapter;
|
||||||
import android.animation.ValueAnimator;
|
import android.animation.ValueAnimator;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.media.AudioFocusRequest;
|
|
||||||
import android.media.AudioManager;
|
import android.media.AudioManager;
|
||||||
import android.media.audiofx.AudioEffect;
|
import android.media.audiofx.AudioEffect;
|
||||||
import android.os.Build;
|
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
import androidx.core.content.ContextCompat;
|
import androidx.core.content.ContextCompat;
|
||||||
|
import androidx.media.AudioFocusRequestCompat;
|
||||||
|
import androidx.media.AudioManagerCompat;
|
||||||
|
|
||||||
import com.google.android.exoplayer2.SimpleExoPlayer;
|
import com.google.android.exoplayer2.SimpleExoPlayer;
|
||||||
import com.google.android.exoplayer2.analytics.AnalyticsListener;
|
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 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 int DUCK_DURATION = 1500;
|
||||||
private static final float DUCK_AUDIO_TO = .2f;
|
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 static final int STREAM_TYPE = AudioManager.STREAM_MUSIC;
|
||||||
|
|
||||||
private final SimpleExoPlayer player;
|
private final SimpleExoPlayer player;
|
||||||
private final Context context;
|
private final Context context;
|
||||||
private final AudioManager audioManager;
|
private final AudioManager audioManager;
|
||||||
|
|
||||||
private final AudioFocusRequest request;
|
private final AudioFocusRequestCompat request;
|
||||||
|
|
||||||
public AudioReactor(@NonNull final Context context,
|
public AudioReactor(@NonNull final Context context,
|
||||||
@NonNull final SimpleExoPlayer player) {
|
@NonNull final SimpleExoPlayer player) {
|
||||||
|
@ -43,15 +40,11 @@ public class AudioReactor implements AudioManager.OnAudioFocusChangeListener, An
|
||||||
this.audioManager = ContextCompat.getSystemService(context, AudioManager.class);
|
this.audioManager = ContextCompat.getSystemService(context, AudioManager.class);
|
||||||
player.addAnalyticsListener(this);
|
player.addAnalyticsListener(this);
|
||||||
|
|
||||||
if (SHOULD_BUILD_FOCUS_REQUEST) {
|
request = new AudioFocusRequestCompat.Builder(FOCUS_GAIN_TYPE)
|
||||||
request = new AudioFocusRequest.Builder(FOCUS_GAIN_TYPE)
|
//.setAcceptsDelayedFocusGain(true)
|
||||||
.setAcceptsDelayedFocusGain(true)
|
|
||||||
.setWillPauseWhenDucked(true)
|
.setWillPauseWhenDucked(true)
|
||||||
.setOnAudioFocusChangeListener(this)
|
.setOnAudioFocusChangeListener(this)
|
||||||
.build();
|
.build();
|
||||||
} else {
|
|
||||||
request = null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void dispose() {
|
public void dispose() {
|
||||||
|
@ -64,19 +57,11 @@ public class AudioReactor implements AudioManager.OnAudioFocusChangeListener, An
|
||||||
//////////////////////////////////////////////////////////////////////////*/
|
//////////////////////////////////////////////////////////////////////////*/
|
||||||
|
|
||||||
public void requestAudioFocus() {
|
public void requestAudioFocus() {
|
||||||
if (SHOULD_BUILD_FOCUS_REQUEST) {
|
AudioManagerCompat.requestAudioFocus(audioManager, request);
|
||||||
audioManager.requestAudioFocus(request);
|
|
||||||
} else {
|
|
||||||
audioManager.requestAudioFocus(this, STREAM_TYPE, FOCUS_GAIN_TYPE);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void abandonAudioFocus() {
|
public void abandonAudioFocus() {
|
||||||
if (SHOULD_BUILD_FOCUS_REQUEST) {
|
AudioManagerCompat.abandonAudioFocusRequest(audioManager, request);
|
||||||
audioManager.abandonAudioFocusRequest(request);
|
|
||||||
} else {
|
|
||||||
audioManager.abandonAudioFocus(this);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getVolume() {
|
public int getVolume() {
|
||||||
|
@ -88,7 +73,7 @@ public class AudioReactor implements AudioManager.OnAudioFocusChangeListener, An
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getMaxVolume() {
|
public int getMaxVolume() {
|
||||||
return audioManager.getStreamMaxVolume(STREAM_TYPE);
|
return AudioManagerCompat.getStreamMaxVolume(audioManager, STREAM_TYPE);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*//////////////////////////////////////////////////////////////////////////
|
/*//////////////////////////////////////////////////////////////////////////
|
||||||
|
|
Loading…
Reference in New Issue