media: Media3 MediaSession custom error messages

Is there an equivalent in media3 for these?

  • MediaSessionConnector.setCustomErrorMessage() to send a custom error message regardless of player state.
  • MediaSessionConnector.setErrorMessageProvider(PlayerErrorMessageProvider) to convert PlaybackException to a custom error message for connected controllers.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 1
  • Comments: 36 (15 by maintainers)

Commits related to this issue

Most upvoted comments

This is planned for 1.4 which is the next release.

Yeah, I meant returning that from onGetChildren() from the MediaLibrarySession.Callback.

I think the remaining piece of this issue is adding ways for apps to generate their own custom PlaybackException even if there isn’t a PlaybackException coming from the player. This is a remaining of the legacy library that leaks into the new API with this. In media1 policy errors in the MediaBrowserServiceCompat have been reported as fatal exception in the playback state. This wasn’t nicely separated in concerns IMO. So we added the workaround that returning an authentication error from the MediaLibraryCallback.onXyz() triggers a fatal exception in the platform playback state.

So I think regarding the custom PlaybackException there isn’t a need to create a new issue.

I’ll do my best to deliver this asap.

I think Media3 is currently missing an API to send a custom fatal exception in an easy way.

The current version allows to send non-fatal errors as you do and it allows to customize error code and error message of a PlaybackException. However, there isn’t an easy way do originate a fatal PlaybackException that isn’t coming from the Player.

https://developer.android.com/media/media3/session/control-playback#error-handling

Sorry for not having a better answer than that we should prioritize this to provide this with the next release.

  • The consumer for MediaSessionConnector.setCustomErrorMessage() is AndroidAuto. It sends respective error messages to be displayed on the AndroidAuto screen when something goes wrong with a play action. (for example user not logged in, no book available for resumption, playback error custom message). Right now with media3, the error message just says “Source error” and I want it to be more user friendly.

  • The consumer for MediaSessionConnector.setErrorMessageProvider(PlayerErrorMessageProvider) is the MediaControllerCompat in my app (I haven´t migrated that part to media3 yet). I map the PlaybackException to a respective error message that is user friendly, and display it in an alert dialog. I see that media3 MediaController has a onPlayerError(PlaybackException) so I am guessing that could be the solution and handle the PlaybackException on the MediaBrowser side.

https://github.com/androidx/media/commit/5d5a430756cdd42e5ae80727b36b8a8fe151085c https://github.com/androidx/media/commit/317b40f26961c6c4db630eea618c612ac038d260

We’ve added MediaSession.setPlaybackException(ControllerInfo, PlaybackException) that allows setting custom PlaybackException for selected or all controllers. Please see the JavaDoc in the main branch for now to learnhow to set a fatal error.

I’m closing this issue. A Media3 app can now 1) send non-fatal policy errors that are not affecting playback, 2) rewrite and change error codes and message of actual PlaybackException instances thrown by the player and 3) set custom PlaybackException for a certain Media3 controller (use the media notification controller to set an error for the legacy session).

We will add documentation for 3) to the official documentation on this page, once we have released that change.

The change will be included in the next release for which the first alpha has been cut already.

If you find an problem with this or have some further input, please file another issue and describe the issue as usual as per issue template.

Yeah, sorry no news yet.

I don’t think this will be part of 1.3 I’m afraid. We will update this issue as soon as we pushed a change that adds this.

no problems with asking questions, I’m glad to help 😃

first of all, the impact of the non-fatal you are describing above with the session.sendError. since we cannot provide the media items for the unauthorized user, we return an empty list

    override fun onAddMediaItems(
        mediaSession: MediaSession,
        controller: MediaSession.ControllerInfo,
        mediaItems: MutableList<MediaItem>
    ): ListenableFuture<MutableList<MediaItem>> = coroutineScope.future {
        if (isUnauthorized) {
            mediaSession.sendError(
                controller,
                SessionError(
                    SessionError.ERROR_SESSION_AUTHENTICATION_EXPIRED,
                    "You are unauthorized"
                )
            )
            return@future mutableListOf()
        }

        val items = androidAutoCallbackHandler.addMediaItems(mediaItems)
        return@future items.toMutableList()
    }

this will result in a stuck “Getting your selection” screen (unless you tap the back button), and an error toast shown at the bottom for a couple of seconds:

389551322-702d7148-bd33-4c25-bf94-c6d3e30e5753

this approach has a serious flaw when used from onAddMediaItems: this error is displayed for a short period time, while the driver might be unable to give a quick look at the AA display due to the traffic or whatever. this means that once they finally are able to take a look at why the music has stopped, they won’t see any error message. in terms of Google Play Store reviewers, this is unacceptable

You kept the listing, then the user clicked and you created an error with PlaybackStateCompat. I think that’s what you did.

yes, that’s correct. here’s a flashback to our previous media2 implementation. things were simpler since we could hack into session.sessionCompat and set the error playback state ourselves. the code for this used to look smth like this:

    @SuppressLint("RestrictedApi")
    @WorkerThread
    override fun onSetMediaUri(
        session: MediaSession,
        controller: MediaSession.ControllerInfo,
        uri: Uri,
        extras: Bundle?
    ): Int {
        runBlocking {
            if (isUnauthorized) {
                val errorState = PlaybackStateCompat.Builder()
                    .setState(PlaybackStateCompat.STATE_ERROR, 0, 0f)
                    .setErrorMessage(PlaybackStateCompat.ERROR_CODE_AUTHENTICATION_EXPIRED, "You are unauthorized")
                    .build()

                session.sessionCompat.setPlaybackState(errorState)
            } else {
                someOurHelper.play(uri)
            }
        }
        return SessionResult.RESULT_SUCCESS
    }

this would result in opening a fullscreen AA player with the error state that would persist until the user took some action (e.g. navigating back and playing smth else)

389510808-9cfbcd08-0c9e-47bd-9c7e-15a03442871f

this behavior based on the following guidelines: https://developer.android.com/training/cars/media#errors

please let me know if you need any additional input from me

Thanks for the quick reply, with our old ExoPlayer implementation we would authenticate for playback, if it failed we would call mediaSessionConnector.setCustomErrorMessage(errorMessage). That resulted in a message being shown in the android auto ui (see attached screenshot).

With media3, we authenticate for playback in onSetMediaItems (when it’s called from an Android auto controller). The difference here is calling mediaSession.sendError() displays a toast instead and the screen remains in “Getting your selection” (as shown in first screenshot). I’ve tried returning a Futures.immediateFailedFuture in onSetMediaItems but it doesn’t have any effect. The only way I can get the screen to show the “Source error” is by returning a broken MediaItem that can’t be played.

Screenshot 2024-08-21 at 12 06 36

We are currently preparing the beta01. The stable release of 1.4.0 is expected to be in mid July.

Our app update got rejected in playstore review because we didn’t send error messages when using android auto. In other words this should be a blocker for many media app(s).

Yes, this is a regression compared with what’s possible with the legacy API. We need to look into this. Marked as enhancement.