From f9bfd268a5dd72f49af565a09509756e009afb01 Mon Sep 17 00:00:00 2001 From: Yubi Lee Date: Sat, 28 Mar 2026 09:14:30 +0900 Subject: [PATCH] Fix broken mute It should play video with mute on even though another app is playing audio. --- .../org/schabi/newpipe/player/Player.java | 27 +++++++++++++++++++ .../newpipe/player/helper/AudioReactor.java | 25 +++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/app/src/main/java/org/schabi/newpipe/player/Player.java b/app/src/main/java/org/schabi/newpipe/player/Player.java index 0605b262a..7ce5244fa 100644 --- a/app/src/main/java/org/schabi/newpipe/player/Player.java +++ b/app/src/main/java/org/schabi/newpipe/player/Player.java @@ -1328,7 +1328,34 @@ 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( + 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(); + } UIs.call(playerUi -> playerUi.onMuteUnmuteChanged(!wasMuted)); notifyPlaybackUpdateToListeners(); } 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 2ec33dc0f..a0cfea2c4 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 @@ -4,9 +4,11 @@ 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,11 +18,13 @@ 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) { @@ -28,13 +32,34 @@ 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 //////////////////////////////////////////////////////////////////////////*/