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

Can't send a built-in session command from .addNotificationAction #107

Closed
1 task
tzugen opened this issue Jul 5, 2022 · 3 comments
Closed
1 task

Can't send a built-in session command from .addNotificationAction #107

tzugen opened this issue Jul 5, 2022 · 3 comments

Comments

@tzugen
Copy link

tzugen commented Jul 5, 2022

Media3 Version

1.0.0-beta01

Devices that reproduce the issue

All

Devices that do not reproduce the issue

No response

Reproducible in the demo app?

Not tested

Reproduction steps

We want to add a custom "like/unlike" button to the notification.

override fun addNotificationActions(
    mediaSession: MediaSession,
    mediaButtons: MutableList<CommandButton>,
    builder: NotificationCompat.Builder,
    actionFactory: MediaNotification.ActionFactory
): IntArray {
    val newCommands: MutableList<CommandButton> = mutableListOf()
    val rating = mediaPlayerController.currentMediaItem?.mediaMetadata?.userRating

    newCommands.add(
        CommandButton.Builder()
            .setDisplayName("Love")
            .setIconResId(
                R.drawable.ic_star_full_dark
            )
            .setSessionCommand(
                SessionCommand(
                    COMMAND_CODE_SESSION_SET_RATING
                )
            )
            .setEnabled(true)
            .build()
    )

    return super.addNotificationActions(
        mediaSession,
        mediaButtons + newCommands,
        builder,
        actionFactory
    )
}

Expected result

The button is added correctly.

Actual result

A crash at

java.lang.IllegalArgumentException
    at androidx.media3.common.util.Assertions.checkArgument(Assertions.java:40)
    at androidx.media3.session.DefaultActionFactory.createCustomActionFromCustomCommandButton(DefaultActionFactory.java:88)
    at androidx.media3.session.DefaultMediaNotificationProvider.addNotificationActions(DefaultMediaNotificationProvider.java:318)
    at org.moire.ultrasonic.playback.MediaNotificationProvider.addNotificationActions(MediaNotificationProvider.kt:62)

There is a check that enforces only custom session commands to be used. Why is it there?
We would like to use the built-in command...

Media

--

Bug Report

@marcbaechinger
Copy link
Contributor

marcbaechinger commented Jul 5, 2022

In general I think the best approach is to use MediaSession.Callback.onCustomCommand and MediaSession.setCustomLayout(). If you do that, the DefaultMediaNotificationProvider will handle this for you and then make sure that this works on Android 12 and earlier as well as on Android 13.

You can see how this is done in the demo app. There is also issue #38 that explains this in a bit more detail.

The customLayout is replicated in the legacy session as custom actions. This way the library is handling that for you. There are two types of command: a) player commands, these are commands that the library can handle themselves because it's clear what Player method to call. b) Then there's all other commands that need the app to do something. All of these are custom commands and need to be replicated in the legacy session as 'custom actions'. You want to see these custom actions on Android 12 and earlier as notification actions, and from Android 13, these actions need to be converted to custom actions in the legacy session.

The library does all these things for you. The price you pay for this is that you need to accept to do this as a custom command. If you do that no changes are needed regarding the DefaultMediaNotificationProvider. You only need to join the customLayout concept. The alternative is to implement this all on your end which is possible, but you are rewriting a good amount of code that the library would provide.

@tzugen
Copy link
Author

tzugen commented Jul 6, 2022

Thanks for explaining!

@marcbaechinger
Copy link
Contributor

I'm closing this issue. Please re-open or open a new one if this is required. Happy to help.

@androidx androidx locked and limited conversation to collaborators Mar 3, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants