Skip to content

Commit e32822e

Browse files
utafraliclaude
andcommitted
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 <noreply@anthropic.com>
1 parent 9a292e3 commit e32822e

File tree

1 file changed

+1
-32
lines changed

1 file changed

+1
-32
lines changed

app/src/main/java/org/schabi/newpipe/player/helper/AudioReactor.java

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
package org.schabi.newpipe.player.helper;
22

3-
import android.animation.Animator;
4-
import android.animation.AnimatorListenerAdapter;
5-
import android.animation.ValueAnimator;
63
import android.content.Context;
74
import android.content.Intent;
85
import android.media.AudioManager;
@@ -21,7 +18,6 @@ public class AudioReactor implements AudioManager.OnAudioFocusChangeListener, An
2118

2219
private static final String TAG = "AudioFocusReactor";
2320

24-
private static final int DUCK_DURATION = 1500;
2521
private static final float DUCK_AUDIO_TO = .2f;
2622

2723
private static final int FOCUS_GAIN_TYPE = AudioManagerCompat.AUDIOFOCUS_GAIN;
@@ -42,7 +38,6 @@ public AudioReactor(@NonNull final Context context,
4238

4339
request = new AudioFocusRequestCompat.Builder(FOCUS_GAIN_TYPE)
4440
//.setAcceptsDelayedFocusGain(true)
45-
.setWillPauseWhenDucked(true)
4641
.setOnAudioFocusChangeListener(this)
4742
.build();
4843
}
@@ -100,8 +95,7 @@ public void onAudioFocusChange(final int focusChange) {
10095

10196
private void onAudioFocusGain() {
10297
Log.d(TAG, "onAudioFocusGain() called");
103-
player.setVolume(DUCK_AUDIO_TO);
104-
animateAudio(DUCK_AUDIO_TO, 1.0f);
98+
player.setVolume(1.0f);
10599

106100
if (PlayerHelper.isResumeAfterAudioFocusGain(context)) {
107101
player.play();
@@ -119,31 +113,6 @@ private void onAudioFocusLossCanDuck() {
119113
player.setVolume(DUCK_AUDIO_TO);
120114
}
121115

122-
private void animateAudio(final float from, final float to) {
123-
final ValueAnimator valueAnimator = new ValueAnimator();
124-
valueAnimator.setFloatValues(from, to);
125-
valueAnimator.setDuration(AudioReactor.DUCK_DURATION);
126-
valueAnimator.addListener(new AnimatorListenerAdapter() {
127-
@Override
128-
public void onAnimationStart(final Animator animation) {
129-
player.setVolume(from);
130-
}
131-
132-
@Override
133-
public void onAnimationCancel(final Animator animation) {
134-
player.setVolume(to);
135-
}
136-
137-
@Override
138-
public void onAnimationEnd(final Animator animation) {
139-
player.setVolume(to);
140-
}
141-
});
142-
valueAnimator.addUpdateListener(animation ->
143-
player.setVolume(((float) animation.getAnimatedValue())));
144-
valueAnimator.start();
145-
}
146-
147116
/*//////////////////////////////////////////////////////////////////////////
148117
// Audio Processing
149118
//////////////////////////////////////////////////////////////////////////*/

0 commit comments

Comments
 (0)