Commit 826039ad by bachinger Committed by Ian Baker

Support chronometer for notifications without seekbar for lower APIs

This appears on the notifcation as the elapsed duration like '2:12'
as we had this with the PlayerNotifcationtManager. Notifications on
recent API versions show a seekbar and duration based on the media
session playback state and ignore the chronometer.

PiperOrigin-RevId: 445110202
parent 564dc11f
......@@ -31,6 +31,7 @@ import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;
import androidx.core.graphics.drawable.IconCompat;
import androidx.media.app.NotificationCompat.MediaStyle;
import androidx.media3.common.C;
import androidx.media3.common.MediaMetadata;
import androidx.media3.common.Player;
import androidx.media3.common.util.Consumer;
......@@ -197,6 +198,13 @@ public final class DefaultMediaNotificationProvider implements MediaNotification
mediaStyle.setShowActionsInCompactView(skipToPreviousAdded ? 1 : 0);
}
long playbackStartTimeMs = getPlaybackStartTimeEpochMs(mediaController);
boolean displayElapsedTimeWithChronometer = playbackStartTimeMs != C.TIME_UNSET;
builder
.setWhen(playbackStartTimeMs)
.setShowWhen(displayElapsedTimeWithChronometer)
.setUsesChronometer(displayElapsedTimeWithChronometer);
Notification notification =
builder
.setContentIntent(mediaController.getSessionActivity())
......@@ -272,6 +280,19 @@ public final class DefaultMediaNotificationProvider implements MediaNotification
}
}
private static long getPlaybackStartTimeEpochMs(MediaController controller) {
// Changing "showWhen" causes notification flicker if SDK_INT < 21.
if (Util.SDK_INT >= 21
&& controller.isPlaying()
&& !controller.isPlayingAd()
&& !controller.isCurrentMediaItemDynamic()
&& controller.getPlaybackParameters().speed == 1f) {
return System.currentTimeMillis() - controller.getContentPosition();
} else {
return C.TIME_UNSET;
}
}
private static class OnBitmapLoadedFutureCallback implements FutureCallback<Bitmap> {
private final Consumer<Bitmap> consumer;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment