From e32822e5b7595c0cc3078f494cb985cca9014b94 Mon Sep 17 00:00:00 2001 From: ugurtafrali Date: Mon, 23 Feb 2026 05:08:21 +0300 Subject: [PATCH] Fix #13283: restore volume immediately on audio focus gain Replace the ValueAnimator-based animateAudio() call in onAudioFocusGain() with a direct player.setVolume(1.0f). The ValueAnimator relies on the Choreographer/main thread rendering loop, which is throttled or suspended when the app is backgrounded (especially with battery optimizations on Android 12+). This left the volume stuck at 0.2f until the user foregrounded the app. Also remove setWillPauseWhenDucked(true) since the app ducks volume rather than pausing, aligning the declared intent with actual behavior so the system reliably delivers AUDIOFOCUS_GAIN on newer Android versions. Co-Authored-By: Claude Sonnet 4.6 --- .../newpipe/player/helper/AudioReactor.java | 33 +------------------ 1 file changed, 1 insertion(+), 32 deletions(-) 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 084336d5483..5fc99e71425 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 @@ -1,8 +1,5 @@ package org.schabi.newpipe.player.helper; -import android.animation.Animator; -import android.animation.AnimatorListenerAdapter; -import android.animation.ValueAnimator; import android.content.Context; import android.content.Intent; import android.media.AudioManager; @@ -21,7 +18,6 @@ public class AudioReactor implements AudioManager.OnAudioFocusChangeListener, An private static final String TAG = "AudioFocusReactor"; - private static final int DUCK_DURATION = 1500; private static final float DUCK_AUDIO_TO = .2f; private static final int FOCUS_GAIN_TYPE = AudioManagerCompat.AUDIOFOCUS_GAIN; @@ -42,7 +38,6 @@ public AudioReactor(@NonNull final Context context, request = new AudioFocusRequestCompat.Builder(FOCUS_GAIN_TYPE) //.setAcceptsDelayedFocusGain(true) - .setWillPauseWhenDucked(true) .setOnAudioFocusChangeListener(this) .build(); } @@ -100,8 +95,7 @@ public void onAudioFocusChange(final int focusChange) { private void onAudioFocusGain() { Log.d(TAG, "onAudioFocusGain() called"); - player.setVolume(DUCK_AUDIO_TO); - animateAudio(DUCK_AUDIO_TO, 1.0f); + player.setVolume(1.0f); if (PlayerHelper.isResumeAfterAudioFocusGain(context)) { player.play(); @@ -119,31 +113,6 @@ private void onAudioFocusLossCanDuck() { player.setVolume(DUCK_AUDIO_TO); } - private void animateAudio(final float from, final float to) { - final ValueAnimator valueAnimator = new ValueAnimator(); - valueAnimator.setFloatValues(from, to); - valueAnimator.setDuration(AudioReactor.DUCK_DURATION); - valueAnimator.addListener(new AnimatorListenerAdapter() { - @Override - public void onAnimationStart(final Animator animation) { - player.setVolume(from); - } - - @Override - public void onAnimationCancel(final Animator animation) { - player.setVolume(to); - } - - @Override - public void onAnimationEnd(final Animator animation) { - player.setVolume(to); - } - }); - valueAnimator.addUpdateListener(animation -> - player.setVolume(((float) animation.getAnimatedValue()))); - valueAnimator.start(); - } - /*////////////////////////////////////////////////////////////////////////// // Audio Processing //////////////////////////////////////////////////////////////////////////*/