mirror of
https://github.com/TeamNewPipe/NewPipe
synced 2026-11-23 15:47:20 +00:00
Refactor automatic audio focus, drop unneeded manual focus
The automatic audio focus can be just disabled when muting by updating ExoPlayer audio attributes. It makes no sense to create a manual audio focus request which is never used but only unregistered. There should be no need to manually play when content is already playing, to log each action for all build types and to duplicate the code updating ExoPlayer audio attributes.
This commit is contained in:
1 parent
f9bfd268a5
commit
9ff614f2fc
2 files changed
+13
-56
No files matched your search
@@ -634,14 +634,13 @@ public final class Player implements PlaybackListener, Listener {
|
||||
simpleExoPlayer.setWakeMode(C.WAKE_MODE_NETWORK);
|
||||
simpleExoPlayer.setHandleAudioBecomingNoisy(true);
|
||||
|
||||
// Enable automatic audio focus management - let Android handle ducking automatically
|
||||
// Enable ExoPlayer automatic audio focus management
|
||||
simpleExoPlayer.setAudioAttributes(
|
||||
new com.google.android.exoplayer2.audio.AudioAttributes.Builder()
|
||||
.setUsage(C.USAGE_MEDIA)
|
||||
.setContentType(C.AUDIO_CONTENT_TYPE_MUSIC)
|
||||
.build(),
|
||||
true // handleAudioFocus = true for automatic management
|
||||
);
|
||||
true);
|
||||
|
||||
audioReactor = new AudioReactor(context, simpleExoPlayer);
|
||||
|
||||
@@ -1328,34 +1327,19 @@ public final class Player implements PlaybackListener, Listener {
|
||||
|
||||
public void toggleMute() {
|
||||
final boolean wasMuted = isMuted();
|
||||
final boolean wasPlaying = simpleExoPlayer.isPlaying();
|
||||
Log.d(TAG, "toggleMute: wasMuted=" + wasMuted + ", wasPlaying=" + wasPlaying);
|
||||
simpleExoPlayer.setVolume(wasMuted ? 1 : 0);
|
||||
if (wasMuted) {
|
||||
Log.d(TAG, "toggleMute: enabling audio focus, willPlay=" + wasPlaying);
|
||||
simpleExoPlayer.setAudioAttributes(
|
||||
|
||||
// Update audio focus
|
||||
// When wasMuted is true, we want to play sound, so ExoPlayer has to handle audio focus in
|
||||
// this case; when wasMuted is false, we don't want to play sound, so no audio focus should
|
||||
// be done
|
||||
simpleExoPlayer.setAudioAttributes(
|
||||
new com.google.android.exoplayer2.audio.AudioAttributes.Builder()
|
||||
.setUsage(C.USAGE_MEDIA)
|
||||
.setContentType(C.AUDIO_CONTENT_TYPE_MUSIC)
|
||||
.build(),
|
||||
true // handleAudioFocus = true - ExoPlayer handles focus automatically
|
||||
);
|
||||
if (wasPlaying) {
|
||||
Log.d(TAG, "toggleMute: calling play(), isPlaying=" + simpleExoPlayer.isPlaying());
|
||||
simpleExoPlayer.play();
|
||||
Log.d(TAG, "toggleMute: after play(), isPlaying=" + simpleExoPlayer.isPlaying());
|
||||
}
|
||||
} else {
|
||||
Log.d(TAG, "toggleMute: disabling audio focus, abandoning focus");
|
||||
simpleExoPlayer.setAudioAttributes(
|
||||
new com.google.android.exoplayer2.audio.AudioAttributes.Builder()
|
||||
.setUsage(C.USAGE_MEDIA)
|
||||
.setContentType(C.AUDIO_CONTENT_TYPE_MUSIC)
|
||||
.build(),
|
||||
false // handleAudioFocus = false
|
||||
);
|
||||
audioReactor.abandonAudioFocus();
|
||||
}
|
||||
.setUsage(C.USAGE_MEDIA)
|
||||
.setContentType(C.AUDIO_CONTENT_TYPE_MUSIC)
|
||||
.build(),
|
||||
wasMuted);
|
||||
|
||||
UIs.call(playerUi -> playerUi.onMuteUnmuteChanged(!wasMuted));
|
||||
notifyPlaybackUpdateToListeners();
|
||||
}
|
||||
|
||||
@@ -4,11 +4,9 @@ import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.media.AudioManager;
|
||||
import android.media.audiofx.AudioEffect;
|
||||
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.ExoPlayer;
|
||||
@@ -16,15 +14,11 @@ import com.google.android.exoplayer2.analytics.AnalyticsListener;
|
||||
|
||||
public class AudioReactor implements AnalyticsListener {
|
||||
|
||||
private static final String TAG = "AudioFocusReactor";
|
||||
|
||||
private static final int FOCUS_GAIN_TYPE = AudioManagerCompat.AUDIOFOCUS_GAIN;
|
||||
private static final int STREAM_TYPE = AudioManager.STREAM_MUSIC;
|
||||
|
||||
private final ExoPlayer player;
|
||||
private final Context context;
|
||||
private final AudioManager audioManager;
|
||||
private final AudioFocusRequestCompat request;
|
||||
|
||||
public AudioReactor(@NonNull final Context context,
|
||||
@NonNull final ExoPlayer player) {
|
||||
@@ -32,34 +26,13 @@ public class AudioReactor implements AnalyticsListener {
|
||||
this.context = context;
|
||||
this.audioManager = ContextCompat.getSystemService(context, AudioManager.class);
|
||||
player.addAnalyticsListener(this);
|
||||
|
||||
request = new AudioFocusRequestCompat.Builder(FOCUS_GAIN_TYPE)
|
||||
.setWillPauseWhenDucked(true)
|
||||
.setOnAudioFocusChangeListener(focusChange -> {
|
||||
})
|
||||
.build();
|
||||
}
|
||||
|
||||
public void dispose() {
|
||||
abandonAudioFocus();
|
||||
player.removeAnalyticsListener(this);
|
||||
notifyAudioSessionUpdate(false, player.getAudioSessionId());
|
||||
}
|
||||
|
||||
/*//////////////////////////////////////////////////////////////////////////
|
||||
// Audio Focus
|
||||
//////////////////////////////////////////////////////////////////////////*/
|
||||
|
||||
public void requestAudioFocus() {
|
||||
Log.d(TAG, "requestAudioFocus() called");
|
||||
AudioManagerCompat.requestAudioFocus(audioManager, request);
|
||||
}
|
||||
|
||||
public void abandonAudioFocus() {
|
||||
Log.d(TAG, "abandonAudioFocus() called");
|
||||
AudioManagerCompat.abandonAudioFocusRequest(audioManager, request);
|
||||
}
|
||||
|
||||
/*//////////////////////////////////////////////////////////////////////////
|
||||
// Audio Manager
|
||||
//////////////////////////////////////////////////////////////////////////*/
|
||||
|
||||
Reference in new issue
Block a user