Skip to content

Commit

Permalink
feat(player): Pause player when headset is disconnected (#1103)
Browse files Browse the repository at this point in the history
  • Loading branch information
Samfun75 authored Aug 7, 2023
1 parent 2a8699d commit a7067f4
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions app/src/main/java/eu/kanade/tachiyomi/ui/player/PlayerActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ class PlayerActivity : BaseActivity() {

private val playbackStateBuilder = PlaybackStateCompat.Builder()

private lateinit var headsetReceiver: BroadcastReceiver

internal val player get() = binding.player

internal val playerControls get() = binding.playerControls
Expand Down Expand Up @@ -376,6 +378,11 @@ class PlayerActivity : BaseActivity() {
}

playerIsDestroyed = false

registerReceiver(
headsetReceiver,
IntentFilter(AudioManager.ACTION_AUDIO_BECOMING_NOISY),
)
}

private fun setupPlayerControls() {
Expand Down Expand Up @@ -468,15 +475,11 @@ class PlayerActivity : BaseActivity() {
setCallback(
object : MediaSessionCompat.Callback() {
override fun onPlay() {
player.paused = false
playerControls.toggleControls(isTapped = true)
updatePlaybackState()
pauseByIntents(false)
}

override fun onPause() {
player.paused = true
playerControls.toggleControls()
updatePlaybackState(pause = true)
pauseByIntents(true)
}

override fun onSkipToPrevious() {
Expand All @@ -493,6 +496,20 @@ class PlayerActivity : BaseActivity() {
MediaControllerCompat(this, mediaSession).also { mediaController ->
MediaControllerCompat.setMediaController(this, mediaController)
}

headsetReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
if (intent.action == AudioManager.ACTION_AUDIO_BECOMING_NOISY) {
pauseByIntents(true)
}
}
}
}

private fun pauseByIntents(pause: Boolean) {
player.paused = pause
playerControls.toggleControls(!pause)
updatePlaybackState(pause = pause)
}

private fun updatePlaybackState(cachePause: Boolean = false, pause: Boolean = false) {
Expand Down Expand Up @@ -590,6 +607,7 @@ class PlayerActivity : BaseActivity() {
override fun onDestroy() {
mediaSession.isActive = false
mediaSession.release()
unregisterReceiver(headsetReceiver)

playerPreferences.playerVolumeValue().set(fineVolume)
playerPreferences.playerBrightnessValue().set(brightness)
Expand Down

0 comments on commit a7067f4

Please sign in to comment.