Skip to content

Commit

Permalink
refactor(audio focus): support ducking
Browse files Browse the repository at this point in the history
basically copied mpv-android's implementation ¯\_(ツ)_/¯
  • Loading branch information
abdallahmehiz committed Jul 15, 2024
1 parent 6a6023b commit 5aa6d11
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion app/src/main/java/live/mehiz/mpvkt/ui/player/PlayerActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 = {}
}
}
}

Expand Down

0 comments on commit 5aa6d11

Please sign in to comment.