mirror of
https://github.com/TeamNewPipe/NewPipe
synced 2026-11-23 23:57:23 +00:00
Fix broken mute
It should play video with mute on even though another app is playing audio.
This commit is contained in:
1 parent
129b5c2bb1
commit
f9bfd268a5
2 files changed
+52
No files matched your search
@@ -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();
|
||||
}
|
||||
|
||||
@@ -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
|
||||
//////////////////////////////////////////////////////////////////////////*/
|
||||
|
||||
Reference in new issue
Block a user