Skip to content

Commit

Permalink
feat: onMediaButtonEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
lovegaoshi committed Oct 21, 2023
1 parent 49da020 commit 43a23de
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,6 @@ class FirstFragment : Fragment() {
val notificationConfig = NotificationConfig(
listOf(
NotificationButton.PLAY_PAUSE(),
NotificationButton.NEXT(isCompact = true),
NotificationButton.PREVIOUS(isCompact = true),
NotificationButton.BACKWARD(isCompact = true),
NotificationButton.FORWARD(isCompact = true, icon = com.google.android.exoplayer2.ui.R.drawable.exo_icon_circular_play),
NotificationButton.SEEK_TO
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package com.doublesymmetry.kotlinaudio.players

import android.content.Context
import android.content.Intent
import android.media.AudioManager
import android.media.AudioManager.AUDIOFOCUS_LOSS
import android.net.Uri
import android.os.Bundle
import android.os.ResultReceiver
import android.support.v4.media.RatingCompat
import android.support.v4.media.session.MediaSessionCompat
import android.view.KeyEvent
import androidx.annotation.CallSuper
import androidx.core.content.ContextCompat
import androidx.media.AudioAttributesCompat
Expand Down Expand Up @@ -199,6 +201,27 @@ abstract class BaseAudioPlayer internal constructor(
if (playerConfig.interceptPlayerActionsTriggeredExternally) createForwardingPlayer() else exoPlayer

mediaSession.setCallback(object: MediaSessionCompat.Callback() {
// HACK: special for podverse that intercepts SkipNext and SkipPrevious and handles accordingly
// as podverse does not enable these playback capabilities
override fun onMediaButtonEvent(mediaButtonEvent: Intent?): Boolean {
if (mediaButtonEvent?.action == Intent.ACTION_MEDIA_BUTTON) {
val event = mediaButtonEvent.getParcelableExtra<KeyEvent>(Intent.EXTRA_KEY_EVENT)
if (event != null && event.action == KeyEvent.ACTION_DOWN) {
when (event.keyCode) {
KeyEvent.KEYCODE_MEDIA_NEXT -> {
this.onSkipToNext()
return true
}
KeyEvent.KEYCODE_MEDIA_PREVIOUS -> {
this.onSkipToPrevious()
return true
} else -> {
}
}
}
}
return super.onMediaButtonEvent(mediaButtonEvent)
}
override fun onPlayFromMediaId(mediaId: String?, extras: Bundle?) {
Timber.tag("GVATest").d("playing from mediaID: %s", mediaId)
mediaSessionCallback.handlePlayFromMediaId(mediaId, extras)
Expand Down

0 comments on commit 43a23de

Please sign in to comment.