From f1287dda07bcfa5d058879a6fde63edc94398b4b Mon Sep 17 00:00:00 2001 From: svdztn <903183063@qq.com> Date: Sun, 3 Oct 2021 13:49:04 +0800 Subject: [PATCH] handle media button: seekBackward and seekForward --- .../app/PlayerNotificationService.kt | 82 +++++++++++++++++++ 1 file changed, 82 insertions(+) diff --git a/android/app/src/main/java/com/audiobookshelf/app/PlayerNotificationService.kt b/android/app/src/main/java/com/audiobookshelf/app/PlayerNotificationService.kt index 4018dbcd..5a085838 100644 --- a/android/app/src/main/java/com/audiobookshelf/app/PlayerNotificationService.kt +++ b/android/app/src/main/java/com/audiobookshelf/app/PlayerNotificationService.kt @@ -32,6 +32,11 @@ import com.google.android.exoplayer2.source.hls.HlsMediaSource import com.google.android.exoplayer2.ui.PlayerNotificationManager import com.google.android.exoplayer2.upstream.* import kotlinx.coroutines.* +import android.view.KeyEvent +import java.io.File +import java.util.* +import kotlin.concurrent.schedule +import android.annotation.SuppressLint const val NOTIFICATION_LARGE_ICON_SIZE = 144 // px @@ -73,6 +78,10 @@ class PlayerNotificationService : MediaBrowserServiceCompat() { private var audiobooks = mutableListOf() + private var mediaButtonClickCount: Int = 0 + var mediaButtonClickTimeout: Long = 1000 //ms + var seekAmount: Long = 20000 //ms + fun setCustomObjectListener(mylistener: MyCustomObjectListener) { listener = mylistener } @@ -296,6 +305,13 @@ class PlayerNotificationService : MediaBrowserServiceCompat() { //attach player to playerNotificationManager playerNotificationManager.setPlayer(mPlayer) + + mediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS) + mediaSession.setCallback(object : MediaSessionCompat.Callback() { + override fun onMediaButtonEvent(mediaButtonEvent: Intent): Boolean { + return handleCallMediaButton(mediaButtonEvent) + } + }) } private inner class DescriptionAdapter(private val controller: MediaControllerCompat) : @@ -583,5 +599,71 @@ class PlayerNotificationService : MediaBrowserServiceCompat() { } result.sendResult(mediaItems) } + + fun handleCallMediaButton(intent: Intent): Boolean { + if(Intent.ACTION_MEDIA_BUTTON == intent.getAction()) { + var keyEvent = intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT) + if (keyEvent?.getAction() == KeyEvent.ACTION_UP) { + when (keyEvent?.getKeyCode()) { + KeyEvent.KEYCODE_HEADSETHOOK -> { + if(0 == mediaButtonClickCount) { + if (mPlayer.isPlaying) + pause() + else + play() + } + handleMediaButtonClickCount() + } + KeyEvent.KEYCODE_MEDIA_PLAY -> { + if(0 == mediaButtonClickCount) play() + handleMediaButtonClickCount() + } + KeyEvent.KEYCODE_MEDIA_PAUSE -> { + if(0 == mediaButtonClickCount) pause() + handleMediaButtonClickCount() + } + KeyEvent.KEYCODE_MEDIA_NEXT -> { + seekForward(seekAmount) + } + KeyEvent.KEYCODE_MEDIA_PREVIOUS -> { + seekBackward(seekAmount) + } + KeyEvent.KEYCODE_MEDIA_STOP -> { + terminateStream() + } + else -> { + Log.d(tag, "KeyCode:${keyEvent?.getKeyCode()}") + return false + } + } + } + } + return true + } + + fun handleMediaButtonClickCount() { + mediaButtonClickCount++ + if (1 == mediaButtonClickCount) { + Timer().schedule(mediaButtonClickTimeout) { + handler.sendEmptyMessage(mediaButtonClickCount) + mediaButtonClickCount = 0 + } + } + } + + private val handler : Handler = @SuppressLint("HandlerLeak") + object : Handler(){ + override fun handleMessage(msg: Message) { + super.handleMessage(msg) + if (2 == msg.what) { + seekBackward(seekAmount) + play() + } + else if (msg.what >= 3) { + seekForward(seekAmount) + play() + } + } + } }