Fix sometimes seeing outdated thumbnail in notification

Before the thumbnail finishes loading for the new video the player is now playing, the old thumbnail was being used, leading to wrong thumbnails set in the media session and the notification.
This commit is contained in:
Stypox 2022-07-21 23:03:42 +02:00 committed by litetex
parent 3cc43e9fb9
commit f3a9b81b67
2 changed files with 16 additions and 5 deletions

View File

@ -748,11 +748,15 @@ public final class Player implements PlaybackListener, Listener {
//////////////////////////////////////////////////////////////////////////*/
//region Thumbnail loading
private void initThumbnail(final String url) {
private void loadCurrentThumbnail(final String url) {
if (DEBUG) {
Log.d(TAG, "Thumbnail - initThumbnail() called with url = ["
Log.d(TAG, "Thumbnail - loadCurrentThumbnail() called with url = ["
+ (url == null ? "null" : url) + "]");
}
// Unset currentThumbnail, since it is now outdated. This ensures it is not used in media
// session metadata while the new thumbnail is being loaded by Picasso.
currentThumbnail = null;
if (isNullOrEmpty(url)) {
return;
}
@ -762,8 +766,8 @@ public final class Player implements PlaybackListener, Listener {
@Override
public void onBitmapLoaded(final Bitmap bitmap, final Picasso.LoadedFrom from) {
if (DEBUG) {
Log.d(TAG, "Thumbnail - onLoadingComplete() called with: url = [" + url
+ "], " + "loadedImage = [" + bitmap + " -> " + bitmap.getWidth() + "x"
Log.d(TAG, "Thumbnail - onBitmapLoaded() called with: url = [" + url
+ "], " + "bitmap = [" + bitmap + " -> " + bitmap.getWidth() + "x"
+ bitmap.getHeight() + "], from = [" + from + "]");
}
@ -1727,7 +1731,7 @@ public final class Player implements PlaybackListener, Listener {
maybeAutoQueueNextStream(info);
initThumbnail(info.getThumbnailUrl());
loadCurrentThumbnail(info.getThumbnailUrl());
registerStreamViewed();
notifyMetadataUpdateToListeners();

View File

@ -1,10 +1,12 @@
package org.schabi.newpipe.util;
import static org.schabi.newpipe.MainActivity.DEBUG;
import static org.schabi.newpipe.extractor.utils.Utils.isBlank;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;
import androidx.annotation.Nullable;
@ -24,6 +26,7 @@ import java.util.concurrent.TimeUnit;
import okhttp3.OkHttpClient;
public final class PicassoHelper {
private static final String TAG = PicassoHelper.class.getSimpleName();
public static final String PLAYER_THUMBNAIL_TAG = "PICASSO_PLAYER_THUMBNAIL_TAG";
private static final String PLAYER_THUMBNAIL_TRANSFORMATION_KEY =
"PICASSO_PLAYER_THUMBNAIL_TRANSFORMATION_KEY";
@ -129,6 +132,10 @@ public final class PicassoHelper {
.transform(new Transformation() {
@Override
public Bitmap transform(final Bitmap source) {
if (DEBUG) {
Log.d(TAG, "Thumbnail - transform() called");
}
final float notificationThumbnailWidth = Math.min(
context.getResources()
.getDimension(R.dimen.player_notification_thumbnail_width),