From 5aa6d1157bcc45a1e8636759d4dfb2173976e696 Mon Sep 17 00:00:00 2001 From: AbdallahMehiz Date: Mon, 15 Jul 2024 15:29:06 +0100 Subject: [PATCH] =?UTF-8?q?refactor(audio=20focus):=20support=20ducking=20?= =?UTF-8?q?basically=20copied=20mpv-android's=20implementation=20=C2=AF\?= =?UTF-8?q?=5F(=E3=83=84)=5F/=C2=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mehiz/mpvkt/ui/player/PlayerActivity.kt | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/live/mehiz/mpvkt/ui/player/PlayerActivity.kt b/app/src/main/java/live/mehiz/mpvkt/ui/player/PlayerActivity.kt index d18a1fd..f5ec4a2 100644 --- a/app/src/main/java/live/mehiz/mpvkt/ui/player/PlayerActivity.kt +++ b/app/src/main/java/live/mehiz/mpvkt/ui/player/PlayerActivity.kt @@ -50,6 +50,8 @@ class PlayerActivity : AppCompatActivity() { private lateinit var fileName: String + private var restoreAudioFocus: () -> Unit = {} + override fun onCreate(savedInstanceState: Bundle?) { if (playerPreferences.drawOverDisplayCutout.get()) enableEdgeToEdge() super.onCreate(savedInstanceState) @@ -153,7 +155,25 @@ class PlayerActivity : AppCompatActivity() { private val audioFocusChangeListener = AudioManager.OnAudioFocusChangeListener { when (it) { AudioManager.AUDIOFOCUS_LOSS, - AudioManager.AUDIOFOCUS_LOSS_TRANSIENT -> viewModel.pause() + AudioManager.AUDIOFOCUS_LOSS_TRANSIENT -> { + val oldRestore = restoreAudioFocus + val wasPlayerPaused = player.paused ?: false + viewModel.pause() + restoreAudioFocus = { + oldRestore() + if (!wasPlayerPaused) viewModel.unpause() + } + } + AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK -> { + MPVLib.command(arrayOf("multiply", "volume", "0.5")) + restoreAudioFocus = { + MPVLib.command(arrayOf("multiply", "volume", "2")) + } + } + AudioManager.AUDIOFOCUS_GAIN -> { + restoreAudioFocus() + restoreAudioFocus = {} + } } }