From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: alina sireneva <alina@tei.su>
Date: Fri, 17 Jul 2026 21:18:03 +0400
Subject: system-level transient audio focus when playing videos

---
 .../google/android/exoplayer2/AudioFocusManager.java   | 10 ++++++++--
 .../com/google/android/exoplayer2/ExoPlayerImpl.java   |  6 +++++-
 .../java/org/telegram/ui/Components/VideoPlayer.java   |  9 +++++++++
 .../src/main/java/org/telegram/ui/PhotoViewer.java     |  1 +
 4 files changed, 23 insertions(+), 3 deletions(-)

diff --git a/TMessagesProj/src/main/java/com/google/android/exoplayer2/AudioFocusManager.java b/TMessagesProj/src/main/java/com/google/android/exoplayer2/AudioFocusManager.java
index 0000000..0000000 100644
--- a/TMessagesProj/src/main/java/com/google/android/exoplayer2/AudioFocusManager.java
+++ b/TMessagesProj/src/main/java/com/google/android/exoplayer2/AudioFocusManager.java
@@ -244,6 +244,12 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
     }
   }
 
+  public boolean inu_transientFocus;
+
+  private int inu_getFocusGainToRequest() {
+    return inu_transientFocus && focusGainToRequest == AUDIOFOCUS_GAIN ? AUDIOFOCUS_GAIN_TRANSIENT : focusGainToRequest;
+  }
+
   private void abandonAudioFocusIfHeld() {
     if (audioFocusState == AUDIO_FOCUS_STATE_NO_FOCUS) {
       return;
@@ -260,7 +266,7 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
     return audioManager.requestAudioFocus(
         focusListener,
         Util.getStreamTypeForAudioUsage(checkNotNull(audioAttributes).usage),
-        focusGainToRequest);
+        inu_getFocusGainToRequest());
   }
 
   @RequiresApi(26)
@@ -268,7 +274,7 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
     if (audioFocusRequest == null || rebuildAudioFocusRequest) {
       AudioFocusRequest.Builder builder =
           audioFocusRequest == null
-              ? new AudioFocusRequest.Builder(focusGainToRequest)
+              ? new AudioFocusRequest.Builder(inu_getFocusGainToRequest())
               : new AudioFocusRequest.Builder(audioFocusRequest);
 
       boolean willPauseWhenDucked = willPauseWhenDucked();
diff --git a/TMessagesProj/src/main/java/com/google/android/exoplayer2/ExoPlayerImpl.java b/TMessagesProj/src/main/java/com/google/android/exoplayer2/ExoPlayerImpl.java
index 0000000..0000000 100644
--- a/TMessagesProj/src/main/java/com/google/android/exoplayer2/ExoPlayerImpl.java
+++ b/TMessagesProj/src/main/java/com/google/android/exoplayer2/ExoPlayerImpl.java
@@ -112,7 +112,7 @@ import java.util.concurrent.CopyOnWriteArraySet;
 import java.util.concurrent.TimeoutException;
 
 /** The default implementation of {@link ExoPlayer}. */
-/* package */ final class ExoPlayerImpl extends BasePlayer
+public final class ExoPlayerImpl extends BasePlayer
     implements ExoPlayer,
         ExoPlayer.AudioComponent,
         ExoPlayer.VideoComponent,
@@ -1425,6 +1425,10 @@ import java.util.concurrent.TimeoutException;
     listeners.flushEvents();
   }
 
+  public void inu_setTransientAudioFocus(boolean transientFocus) {
+    audioFocusManager.inu_transientFocus = transientFocus;
+  }
+
   @Override
   public AudioAttributes getAudioAttributes() {
     verifyApplicationThread();
diff --git a/TMessagesProj/src/main/java/org/telegram/ui/Components/VideoPlayer.java b/TMessagesProj/src/main/java/org/telegram/ui/Components/VideoPlayer.java
index 0000000..0000000 100644
--- a/TMessagesProj/src/main/java/org/telegram/ui/Components/VideoPlayer.java
+++ b/TMessagesProj/src/main/java/org/telegram/ui/Components/VideoPlayer.java
@@ -275,6 +275,12 @@ public class VideoPlayer implements Player.Listener, VideoListener, AnalyticsLis
             }
             player = builder.build();
 
+            if (handleAudioFocus) {
+                if (player instanceof com.google.android.exoplayer2.ExoPlayerImpl) {
+                    ((com.google.android.exoplayer2.ExoPlayerImpl) player).inu_setTransientAudioFocus(true);
+                }
+                player.setAudioAttributes(player.getAudioAttributes(), true);
+            }
             player.addAnalyticsListener(this);
             player.addListener(this);
             player.addVideoListener(this);
@@ -1631,6 +1637,9 @@ public class VideoPlayer implements Player.Listener, VideoListener, AnalyticsLis
     public void handleAudioFocus(boolean handleAudioFocus) {
         this.handleAudioFocus = handleAudioFocus;
         if (player != null) {
+            if (handleAudioFocus && player instanceof com.google.android.exoplayer2.ExoPlayerImpl) {
+                ((com.google.android.exoplayer2.ExoPlayerImpl) player).inu_setTransientAudioFocus(true);
+            }
             player.setAudioAttributes(player.getAudioAttributes(), handleAudioFocus);
         }
     }
diff --git a/TMessagesProj/src/main/java/org/telegram/ui/PhotoViewer.java b/TMessagesProj/src/main/java/org/telegram/ui/PhotoViewer.java
index 0000000..0000000 100644
--- a/TMessagesProj/src/main/java/org/telegram/ui/PhotoViewer.java
+++ b/TMessagesProj/src/main/java/org/telegram/ui/PhotoViewer.java
@@ -10538,6 +10538,7 @@ public class PhotoViewer implements NotificationCenter.NotificationCenterDelegat
                     }
                 };
                 videoPlayer.setOnQualityChangeListener(this::updateQualityItems);
+                videoPlayer.handleAudioFocus(SharedConfig.pauseMusicOnMedia && currentMessageObject != null && currentMessageObject.isVideo());
                 if (PipUtils.checkPermissions(parentActivity) == PipPermissions.PIP_GRANTED_PIP) {
                     pipSource = new PipSource.Builder(parentActivity, this)
                         .setTagPrefix("photo-viewer-" + videoPlayer.playerId)
