Skip to content
This repository has been archived by the owner on Aug 24, 2023. It is now read-only.

Commit

Permalink
PlayerService: Release audio focus when pausing
Browse files Browse the repository at this point in the history
Signed-off-by: Shinjo Akane <[email protected]>
  • Loading branch information
AkaneTan committed Jul 16, 2023
1 parent 63789b2 commit 4576116
Showing 1 changed file with 10 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ class SymphonicaPlayerService : Service(), MediaPlayer.OnPreparedListener {

private fun pausePlayer() {
musicPlayer!!.pause()
abandonAudioFocus()
broadcastPlayPaused()
}

Expand Down Expand Up @@ -228,6 +229,7 @@ class SymphonicaPlayerService : Service(), MediaPlayer.OnPreparedListener {

"ACTION_PAUSE" -> if (musicPlayer != null && musicPlayer!!.isPlaying) {
musicPlayer!!.pause()
abandonAudioFocus()
broadcastPlayPaused()
}

Expand Down Expand Up @@ -434,11 +436,12 @@ class SymphonicaPlayerService : Service(), MediaPlayer.OnPreparedListener {
updatePlaylistSheetLocation(previousLocation)
}

private val audioFocusRequest: AudioFocusRequest = AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN)
.setOnAudioFocusChangeListener(focusChangeListener)
.build()

private fun requestAudioFocus() {
userRequestedAudioFocus = true
val audioFocusRequest = AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN)
.setOnAudioFocusChangeListener(focusChangeListener)
.build()
controllerViewModel.isSendingRequest = true
val handler = Handler(Looper.getMainLooper())
val runnable = Runnable {
Expand All @@ -449,6 +452,10 @@ class SymphonicaPlayerService : Service(), MediaPlayer.OnPreparedListener {
audioManager.requestAudioFocus(audioFocusRequest)
}

private fun abandonAudioFocus() {
audioManager.abandonAudioFocusRequest(audioFocusRequest)
}

companion object {
var userRequestedAudioFocus = false
var managerSymphonica: NotificationManager? = null
Expand Down

0 comments on commit 4576116

Please sign in to comment.