Remove useless checks before updating metadata
A while ago NewPipe called the metadata update function very often, so checks were needed to ensure not wasting time updating metadata if it were already up to date. Now, instead, the metadata update function is called exactly when needed, i.e. when metadata changes, so such checks are not needed anymore (and were probably also a little resource-heavy).
This commit is contained in:
parent
d73ca41cfe
commit
8bff445ec3
3 changed files with 11 additions and 119 deletions
|
@ -54,7 +54,6 @@ import android.content.Intent;
|
||||||
import android.content.IntentFilter;
|
import android.content.IntentFilter;
|
||||||
import android.content.SharedPreferences;
|
import android.content.SharedPreferences;
|
||||||
import android.graphics.Bitmap;
|
import android.graphics.Bitmap;
|
||||||
import android.graphics.BitmapFactory;
|
|
||||||
import android.graphics.drawable.Drawable;
|
import android.graphics.drawable.Drawable;
|
||||||
import android.media.AudioManager;
|
import android.media.AudioManager;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
@ -1773,10 +1772,6 @@ public final class Player implements PlaybackListener, Listener {
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
public Bitmap getThumbnail() {
|
public Bitmap getThumbnail() {
|
||||||
if (currentThumbnail == null) {
|
|
||||||
currentThumbnail = BitmapFactory.decodeResource(
|
|
||||||
context.getResources(), R.drawable.placeholder_thumbnail_video);
|
|
||||||
}
|
|
||||||
return currentThumbnail;
|
return currentThumbnail;
|
||||||
}
|
}
|
||||||
//endregion
|
//endregion
|
||||||
|
|
|
@ -20,8 +20,6 @@ import org.schabi.newpipe.MainActivity;
|
||||||
import org.schabi.newpipe.player.Player;
|
import org.schabi.newpipe.player.Player;
|
||||||
import org.schabi.newpipe.player.ui.VideoPlayerUi;
|
import org.schabi.newpipe.player.ui.VideoPlayerUi;
|
||||||
|
|
||||||
import java.util.Optional;
|
|
||||||
|
|
||||||
public class MediaSessionManager {
|
public class MediaSessionManager {
|
||||||
private static final String TAG = MediaSessionManager.class.getSimpleName();
|
private static final String TAG = MediaSessionManager.class.getSimpleName();
|
||||||
public static final boolean DEBUG = MainActivity.DEBUG;
|
public static final boolean DEBUG = MainActivity.DEBUG;
|
||||||
|
@ -31,11 +29,6 @@ public class MediaSessionManager {
|
||||||
@NonNull
|
@NonNull
|
||||||
private final MediaSessionConnector sessionConnector;
|
private final MediaSessionConnector sessionConnector;
|
||||||
|
|
||||||
private int lastTitleHashCode;
|
|
||||||
private int lastArtistHashCode;
|
|
||||||
private long lastDuration;
|
|
||||||
private int lastAlbumArtHashCode;
|
|
||||||
|
|
||||||
public MediaSessionManager(@NonNull final Context context,
|
public MediaSessionManager(@NonNull final Context context,
|
||||||
@NonNull final Player player) {
|
@NonNull final Player player) {
|
||||||
mediaSession = new MediaSessionCompat(context, TAG);
|
mediaSession = new MediaSessionCompat(context, TAG);
|
||||||
|
@ -84,134 +77,38 @@ public class MediaSessionManager {
|
||||||
*
|
*
|
||||||
* @param title {@link MediaMetadataCompat#METADATA_KEY_TITLE}
|
* @param title {@link MediaMetadataCompat#METADATA_KEY_TITLE}
|
||||||
* @param artist {@link MediaMetadataCompat#METADATA_KEY_ARTIST}
|
* @param artist {@link MediaMetadataCompat#METADATA_KEY_ARTIST}
|
||||||
* @param optAlbumArt {@link MediaMetadataCompat#METADATA_KEY_ALBUM_ART}
|
* @param albumArt {@link MediaMetadataCompat#METADATA_KEY_ALBUM_ART}, if not null
|
||||||
* @param duration {@link MediaMetadataCompat#METADATA_KEY_DURATION}
|
* @param duration {@link MediaMetadataCompat#METADATA_KEY_DURATION}
|
||||||
* - should be a negative value for unknown durations, e.g. for livestreams
|
* - should be a negative value for unknown durations, e.g. for livestreams
|
||||||
*/
|
*/
|
||||||
public void setMetadata(@NonNull final String title,
|
public void setMetadata(@NonNull final String title,
|
||||||
@NonNull final String artist,
|
@NonNull final String artist,
|
||||||
@NonNull final Optional<Bitmap> optAlbumArt,
|
@Nullable final Bitmap albumArt,
|
||||||
final long duration
|
final long duration) {
|
||||||
) {
|
|
||||||
if (DEBUG) {
|
if (DEBUG) {
|
||||||
Log.d(TAG, "setMetadata called:"
|
Log.d(TAG, "setMetadata called with: title = [" + title + "], artist = [" + artist
|
||||||
+ " t: " + title
|
+ "], albumArt = [" + (albumArt == null ? "null" : albumArt.hashCode())
|
||||||
+ " a: " + artist
|
+ "], duration = [" + duration + "]");
|
||||||
+ " thumb: " + (
|
|
||||||
optAlbumArt.isPresent()
|
|
||||||
? optAlbumArt.get().hashCode()
|
|
||||||
: "<none>")
|
|
||||||
+ " d: " + duration);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!mediaSession.isActive()) {
|
if (!mediaSession.isActive()) {
|
||||||
if (DEBUG) {
|
if (DEBUG) {
|
||||||
Log.d(TAG, "setMetadata: mediaSession not active - exiting");
|
Log.d(TAG, "setMetadata: media session not active, exiting");
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!checkIfMetadataShouldBeSet(title, artist, optAlbumArt, duration)) {
|
|
||||||
if (DEBUG) {
|
|
||||||
Log.d(TAG, "setMetadata: No update required - exiting");
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (DEBUG) {
|
|
||||||
Log.d(TAG, "setMetadata: N_Metadata update:"
|
|
||||||
+ " t: " + title
|
|
||||||
+ " a: " + artist
|
|
||||||
+ " thumb: " + (
|
|
||||||
optAlbumArt.isPresent()
|
|
||||||
? optAlbumArt.get().hashCode()
|
|
||||||
: "<none>")
|
|
||||||
+ " d: " + duration);
|
|
||||||
}
|
|
||||||
|
|
||||||
final MediaMetadataCompat.Builder builder = new MediaMetadataCompat.Builder()
|
final MediaMetadataCompat.Builder builder = new MediaMetadataCompat.Builder()
|
||||||
.putString(MediaMetadataCompat.METADATA_KEY_TITLE, title)
|
.putString(MediaMetadataCompat.METADATA_KEY_TITLE, title)
|
||||||
.putString(MediaMetadataCompat.METADATA_KEY_ARTIST, artist)
|
.putString(MediaMetadataCompat.METADATA_KEY_ARTIST, artist)
|
||||||
.putLong(MediaMetadataCompat.METADATA_KEY_DURATION, duration);
|
.putLong(MediaMetadataCompat.METADATA_KEY_DURATION, duration);
|
||||||
|
|
||||||
if (optAlbumArt.isPresent()) {
|
if (albumArt != null) {
|
||||||
builder.putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART, optAlbumArt.get());
|
builder.putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART, albumArt);
|
||||||
builder.putBitmap(MediaMetadataCompat.METADATA_KEY_DISPLAY_ICON, optAlbumArt.get());
|
builder.putBitmap(MediaMetadataCompat.METADATA_KEY_DISPLAY_ICON, albumArt);
|
||||||
}
|
}
|
||||||
|
|
||||||
mediaSession.setMetadata(builder.build());
|
mediaSession.setMetadata(builder.build());
|
||||||
|
|
||||||
lastTitleHashCode = title.hashCode();
|
|
||||||
lastArtistHashCode = artist.hashCode();
|
|
||||||
lastDuration = duration;
|
|
||||||
optAlbumArt.ifPresent(bitmap -> lastAlbumArtHashCode = bitmap.hashCode());
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean checkIfMetadataShouldBeSet(
|
|
||||||
@NonNull final String title,
|
|
||||||
@NonNull final String artist,
|
|
||||||
@NonNull final Optional<Bitmap> optAlbumArt,
|
|
||||||
final long duration
|
|
||||||
) {
|
|
||||||
// Check if the values have changed since the last time
|
|
||||||
if (title.hashCode() != lastTitleHashCode
|
|
||||||
|| artist.hashCode() != lastArtistHashCode
|
|
||||||
|| duration != lastDuration
|
|
||||||
|| (optAlbumArt.isPresent() && optAlbumArt.get().hashCode() != lastAlbumArtHashCode)
|
|
||||||
) {
|
|
||||||
if (DEBUG) {
|
|
||||||
Log.d(TAG,
|
|
||||||
"checkIfMetadataShouldBeSet: true - reason: changed values since last");
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if the currently set metadata is valid
|
|
||||||
if (getMetadataTitle() == null
|
|
||||||
|| getMetadataArtist() == null
|
|
||||||
// Note that the duration can be <= 0 for live streams
|
|
||||||
) {
|
|
||||||
if (DEBUG) {
|
|
||||||
if (getMetadataTitle() == null) {
|
|
||||||
Log.d(TAG,
|
|
||||||
"N_getMetadataTitle: title == null");
|
|
||||||
} else if (getMetadataArtist() == null) {
|
|
||||||
Log.d(TAG,
|
|
||||||
"N_getMetadataArtist: artist == null");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
// If we got an album art check if the current set AlbumArt is null
|
|
||||||
if (optAlbumArt.isPresent() && getMetadataAlbumArt() == null) {
|
|
||||||
if (DEBUG) {
|
|
||||||
Log.d(TAG, "N_getMetadataAlbumArt: thumb == null");
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Default - no update required
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@Nullable
|
|
||||||
private Bitmap getMetadataAlbumArt() {
|
|
||||||
return mediaSession.getController().getMetadata()
|
|
||||||
.getBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Nullable
|
|
||||||
private String getMetadataTitle() {
|
|
||||||
return mediaSession.getController().getMetadata()
|
|
||||||
.getString(MediaMetadataCompat.METADATA_KEY_TITLE);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Nullable
|
|
||||||
private String getMetadataArtist() {
|
|
||||||
return mediaSession.getController().getMetadata()
|
|
||||||
.getString(MediaMetadataCompat.METADATA_KEY_ARTIST);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -57,7 +57,7 @@ public class MediaSessionPlayerUi extends PlayerUi {
|
||||||
mediaSessionManager.setMetadata(
|
mediaSessionManager.setMetadata(
|
||||||
player.getVideoTitle(),
|
player.getVideoTitle(),
|
||||||
player.getUploaderName(),
|
player.getUploaderName(),
|
||||||
showThumbnail ? Optional.ofNullable(player.getThumbnail()) : Optional.empty(),
|
showThumbnail ? player.getThumbnail() : null,
|
||||||
StreamTypeUtil.isLiveStream(info.getStreamType()) ? -1 : info.getDuration()
|
StreamTypeUtil.isLiveStream(info.getStreamType()) ? -1 : info.getDuration()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue