Handle errors

Android Auto and Android Automotive (AAOS) set the playback state to STATE_ERROR and provide a user-facing, localized error message. The apps can then display the message to the user.

To address an error, you provide an error message with setErrorMessage.

See PlaybackStateCompat for a list of the error codes you can use when designing the error message to indicate the nature of the error. If a user must interact with their phone to resolve an issue, include this instruction in the error message.

Error messages must be user-facing and targeted to the user's locale. For example, if content is not available in the user's locale, use ERROR_CODE_NOT_AVAILABLE_IN_REGION.

Kotlin

mediaSession.setPlaybackState(
    PlaybackStateCompat.Builder()
        .setState(PlaybackStateCompat.STATE_ERROR)
        .setErrorMessage(PlaybackStateCompat.ERROR_CODE_NOT_AVAILABLE_IN_REGION, getString(R.string.error_unsupported_region))
        // ...and any other setters.
        .build())

Java

mediaSession.setPlaybackState(
    new PlaybackStateCompat.Builder()
        .setState(PlaybackStateCompat.STATE_ERROR)
        .setErrorMessage(PlaybackStateCompat.ERROR_CODE_NOT_AVAILABLE_IN_REGION, getString(R.string.error_unsupported_region))
        // ...and any other setters.
        .build())

To learn more about error states, see Using a media session: States and errors.