Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(player): Pause player when headset is disconnected #1103

Merged
merged 1 commit into from
Aug 7, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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