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

Add new custom action #530

Open
wants to merge 3 commits into
base: compose
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import androidx.media3.common.Player.Listener
import androidx.media3.common.util.UnstableApi
import androidx.media3.exoplayer.ExoPlayer
import androidx.media3.exoplayer.util.EventLogger
import androidx.media3.session.CommandButton
import androidx.media3.session.LibraryResult
import androidx.media3.session.MediaLibraryService
import androidx.media3.session.MediaSession
Expand Down Expand Up @@ -326,12 +327,36 @@ open class MusicService : MediaLibraryService() {
.buildUpon()
// Add custom commands
.add(SessionCommand(ACTION_TOGGLE_SPATIALIZATION, Bundle()))
.add(SessionCommand(SEEK_BACK,Bundle()))
.add(SessionCommand(SEEK_FORWARD,Bundle()))
.build()
return MediaSession.ConnectionResult.accept(
sessionCommands, connectionResult.availablePlayerCommands
)
}

override fun onPostConnect(
session: MediaSession,
controller: MediaSession.ControllerInfo
) {
// Display a button for seek back action
val seekBackButton = CommandButton.Builder()
.setDisplayName("Seek Back")
.setIconResId(R.drawable.media3_notification_seek_back)
.setSessionCommand(SessionCommand(SEEK_BACK,Bundle()))
.build()

// Display a button for seek forward action
val seekForwardButton = CommandButton.Builder()
.setDisplayName("Seek Forward")
.setIconResId(R.drawable.media3_notification_seek_forward)
.setSessionCommand(SessionCommand(SEEK_FORWARD,Bundle()))
.build()
session.setCustomLayout(listOf(seekBackButton,seekForwardButton))

super.onPostConnect(session, controller)
}

override fun onGetLibraryRoot(
session: MediaLibrarySession,
browser: MediaSession.ControllerInfo,
Expand Down Expand Up @@ -470,6 +495,16 @@ open class MusicService : MediaLibraryService() {

return Futures.immediateFuture(SessionResult(SessionResult.RESULT_SUCCESS))
}

if (customCommand.customAction == SEEK_BACK) {
exoPlayer.seekBack()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These commands are replicating functionality that's already handled by standard commands defined by a Player, which we don't want to encourage (see the note at the end of this section) - could you update these commands to do something different? Here are some ideas:

return Futures.immediateFuture(SessionResult(SessionResult.RESULT_SUCCESS))
}

if (customCommand.customAction == SEEK_FORWARD) {
exoPlayer.seekForward()
return Futures.immediateFuture(SessionResult(SessionResult.RESULT_SUCCESS))
}
return Futures.immediateFuture(SessionResult(SessionResult.RESULT_ERROR_NOT_SUPPORTED))
}
}
Expand Down Expand Up @@ -534,7 +569,8 @@ private const val CONTENT_STYLE_GRID = 2

const val ACTION_TOGGLE_SPATIALIZATION = "com.example.android.uamp.ACTION_TOGGLE_SPATIALIZATION"
const val EXTRAS_TOGGLE_SPATIALIZATION = "com.example.android.uamp.EXTRAS_TOGGLE_SPATIALIZATION"
const val SEEK_FORWARD = "com.example.android.uamp.SEEK_FORWARD"
const val SEEK_BACK = "com.example.android.uamp.SEEK_BACK"

const val MEDIA_DESCRIPTION_EXTRAS_START_PLAYBACK_POSITION_MS = "playback_start_position_ms"

private const val TAG = "MusicService"