MediaStyle Notification progress bar keeps advancing when track is paused API 30

952 views Asked by At

When I pause from the notification, the progress bar keeps advancing as it was still playing. If I press play again, the progress bar goes back to the position it was originally paused and resumes correctly. The problem comes when I pause: the progress bar doesn't stop and keeps andvancing. Everytime my media player detects a playbackstate change, I first update the PlaybackStateCompat, then I update the MediaMetadataCompat and I finally show the notification. Everything was working fine this way until we ran the application in API 30, which features a progress bar in the expanded notification. In this image, the playback state is paused, but the progress bar is advancing.

enter image description here

This is how I create the PlaybackStateCompat.Builder. The documentation says that you should set playbackSpeed to 0 when paused, and that's exactly what I'm doing.

@Override
public PlaybackStateCompat.Builder getPlaybackState(int typeMetadata, @PlaybackStateCompat.State int playbackStateCompact, MediaInfo mediaInfo,
        long position) {
    PlaybackStateCompat.Builder stateBuilder = new PlaybackStateCompat.Builder();
    switch (typeMetadata) {
        case MetaDataType.MEDIAINFO:
            stateBuilder.setActions(getIconButtons());
            stateBuilder.setExtras(getPlaybackStateExtras(mediaInfo));

            if (mediaInfo.getMediaType() == MediaInfo.MediaType.RADIO) {
                stateBuilder.setState(playbackStateCompact, PlaybackStateCompat.PLAYBACK_POSITION_UNKNOWN, 1.0f);
            } else {
                if (playbackStateCompact == PlaybackStateCompat.STATE_PAUSED) {
                    stateBuilder.setState(playbackStateCompact, position, 0.0f);
                } else if (position == 0) {
                    stateBuilder.setState(playbackStateCompact, PlaybackStateCompat.PLAYBACK_POSITION_UNKNOWN, 1.0f);
                } else {
                    stateBuilder.setState(playbackStateCompact, position, 1.0f, SystemClock.elapsedRealtime());
                }
            }
            return stateBuilder;
        case MetaDataType.AD:
        case MetaDataType.EMPTY:
            stateBuilder.setActions(PlaybackStateCompat.ACTION_PREPARE);
            stateBuilder.setBufferedPosition(0);
            stateBuilder.setState(PlaybackStateCompat.STATE_BUFFERING, PlaybackStateCompat.PLAYBACK_POSITION_UNKNOWN, 1);
            break;
    }
    return stateBuilder;
}

This is how I create the MediaMetadataCompat.Builder

@Override
public MediaMetadataCompat.Builder getMediaCompact(int typeMetadata, MediaInfo mediaInfo) {

    final MediaMetadataCompat.Builder mediaBuilder = new MediaMetadataCompat.Builder();
    switch (typeMetadata) {
        case MetaDataType.MEDIAINFO:
            if (mediaBuilder != null) {
                mediaBuilder.putString(MediaMetadataCompat.METADATA_KEY_MEDIA_ID, mediaInfo.getId());
                mediaBuilder.putString(MediaMetadataCompat.METADATA_KEY_TITLE, mediaInfo.getTitle());
                mediaBuilder.putString(MediaMetadataCompat.METADATA_KEY_ARTIST, mediaInfo.getSubtitle());
                mediaBuilder.putString(MediaMetadataCompat.METADATA_KEY_ALBUM, mediaInfo.getAlbum());
                mediaBuilder.putLong(MediaMetadataCompat.METADATA_KEY_DURATION, mediaInfo.getDuration());
                mediaBuilder.putString(MediaMetadataCompat.METADATA_KEY_ALBUM_ART_URI, ImageManager.getImageUrl(mediaInfo.getCoverPhoto()));
            }
            break;
        case MetaDataType.AD:
            mediaBuilder.putString(MediaMetadataCompat.METADATA_KEY_MEDIA_ID, "");
            mediaBuilder.putString(MediaMetadataCompat.METADATA_KEY_TITLE, ApaManager.getInstance().getMetadata().getString("ad_title_banner"));
            mediaBuilder.putString(MediaMetadataCompat.METADATA_KEY_ARTIST, "");
            mediaBuilder.putString(MediaMetadataCompat.METADATA_KEY_ALBUM, "");
            mediaBuilder.putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART,
                                   BitmapFactory.decodeResource(MyApplication.getAppContext().getResources(), R.drawable.place_holder_playlists));
            break;
        case MetaDataType.EMPTY:
            mediaBuilder.putString(MediaMetadataCompat.METADATA_KEY_MEDIA_ID, "");
            mediaBuilder.putString(MediaMetadataCompat.METADATA_KEY_TITLE, "");
            mediaBuilder.putString(MediaMetadataCompat.METADATA_KEY_ARTIST, "");
            mediaBuilder.putString(MediaMetadataCompat.METADATA_KEY_ALBUM, "");
            mediaBuilder.putString(MediaMetadataCompat.METADATA_KEY_ALBUM_ART_URI, "");
            break;
    }
    return mediaBuilder;
}
1

There are 1 answers

0
FerDensetsu On

The problem was that method getPlaybackState was receiving the wrong @PlaybackStateCompat.State when called.