Optimize session artwork

release^2
jhengazuki 2 months ago
parent 603290d196
commit 6e56c0f4e1
  1. 1
      app/src/leanback/java/com/fongmi/android/tv/ui/activity/CastActivity.java
  2. 1
      app/src/leanback/java/com/fongmi/android/tv/ui/activity/LiveActivity.java
  3. 1
      app/src/leanback/java/com/fongmi/android/tv/ui/activity/VideoActivity.java
  4. 30
      app/src/main/java/com/fongmi/android/tv/player/Players.java
  5. 49
      app/src/main/java/com/fongmi/android/tv/service/PlaybackService.java

@ -324,7 +324,6 @@ public class CastActivity extends BaseActivity implements CustomKeyDownVod.Liste
setState(RenderState.STOPPED); setState(RenderState.STOPPED);
break; break;
case PlayerEvent.TRACK: case PlayerEvent.TRACK:
setMetadata();
setTrackVisible(); setTrackVisible();
mClock.setCallback(this); mClock.setCallback(this);
break; break;

@ -755,7 +755,6 @@ public class LiveActivity extends BaseActivity implements GroupPresenter.OnClick
checkNext(); checkNext();
break; break;
case PlayerEvent.TRACK: case PlayerEvent.TRACK:
setMetadata();
setTrackVisible(); setTrackVisible();
break; break;
case PlayerEvent.SIZE: case PlayerEvent.SIZE:

@ -1074,7 +1074,6 @@ public class VideoActivity extends BaseActivity implements CustomKeyDownVod.List
checkEnded(true); checkEnded(true);
break; break;
case PlayerEvent.TRACK: case PlayerEvent.TRACK:
setMetadata();
setTrackVisible(); setTrackVisible();
mClock.setCallback(this); mClock.setCallback(this);
break; break;

@ -8,6 +8,7 @@ import android.app.Activity;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.media.AudioManager; import android.media.AudioManager;
@ -31,6 +32,7 @@ import androidx.media3.exoplayer.drm.FrameworkMediaDrm;
import androidx.media3.exoplayer.util.EventLogger; import androidx.media3.exoplayer.util.EventLogger;
import androidx.media3.ui.PlayerView; import androidx.media3.ui.PlayerView;
import com.bumptech.glide.Glide;
import com.fongmi.android.tv.App; import com.fongmi.android.tv.App;
import com.fongmi.android.tv.Constant; import com.fongmi.android.tv.Constant;
import com.fongmi.android.tv.R; import com.fongmi.android.tv.R;
@ -546,14 +548,6 @@ public class Players implements Player.Listener, ParseCallback {
return scheme.isEmpty() || "file".equals(scheme) ? !Path.exists(url) : host.isEmpty(); return scheme.isEmpty() || "file".equals(scheme) ? !Path.exists(url) : host.isEmpty();
} }
private MediaMetadataCompat.Builder putBitmap(MediaMetadataCompat.Builder builder, Drawable drawable) {
try {
return builder.putBitmap(MediaMetadataCompat.METADATA_KEY_ART, ((BitmapDrawable) drawable).getBitmap());
} catch (Exception ignored) {
return builder;
}
}
public void setMetadata(String title, String artist, String artUri, Drawable drawable) { public void setMetadata(String title, String artist, String artUri, Drawable drawable) {
MediaMetadataCompat.Builder builder = new MediaMetadataCompat.Builder(); MediaMetadataCompat.Builder builder = new MediaMetadataCompat.Builder();
builder.putString(MediaMetadataCompat.METADATA_KEY_TITLE, title); builder.putString(MediaMetadataCompat.METADATA_KEY_TITLE, title);
@ -562,8 +556,24 @@ public class Players implements Player.Listener, ParseCallback {
builder.putString(MediaMetadataCompat.METADATA_KEY_ALBUM_ART_URI, artUri); builder.putString(MediaMetadataCompat.METADATA_KEY_ALBUM_ART_URI, artUri);
builder.putString(MediaMetadataCompat.METADATA_KEY_DISPLAY_ICON_URI, artUri); builder.putString(MediaMetadataCompat.METADATA_KEY_DISPLAY_ICON_URI, artUri);
builder.putLong(MediaMetadataCompat.METADATA_KEY_DURATION, getDuration()); builder.putLong(MediaMetadataCompat.METADATA_KEY_DURATION, getDuration());
session.setMetadata(putBitmap(builder, drawable).build()); App.execute(() -> putBitmap(builder, drawable));
ActionEvent.update(); }
private void putBitmap(MediaMetadataCompat.Builder builder, Drawable drawable) {
if (drawable == null) {
session.setMetadata(builder.build());
ActionEvent.update();
return;
}
try {
Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
bitmap = Glide.with(App.get()).asBitmap().load(bitmap).submit(512, 512).get();
builder.putBitmap(MediaMetadataCompat.METADATA_KEY_ART, bitmap);
session.setMetadata(builder.build());
ActionEvent.update();
} catch (Exception e) {
e.printStackTrace();
}
} }
public void share(Activity activity, CharSequence title) { public void share(Activity activity, CharSequence title) {

@ -1,5 +1,6 @@
package com.fongmi.android.tv.service; package com.fongmi.android.tv.service;
import android.annotation.SuppressLint;
import android.app.Notification; import android.app.Notification;
import android.app.Service; import android.app.Service;
import android.content.Intent; import android.content.Intent;
@ -8,7 +9,6 @@ import android.graphics.Bitmap;
import android.os.Build; import android.os.Build;
import android.os.IBinder; import android.os.IBinder;
import android.support.v4.media.MediaMetadataCompat; import android.support.v4.media.MediaMetadataCompat;
import android.util.LruCache;
import androidx.annotation.DrawableRes; import androidx.annotation.DrawableRes;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
@ -21,13 +21,11 @@ import androidx.media.app.NotificationCompat.MediaStyle;
import androidx.media.session.MediaButtonReceiver; import androidx.media.session.MediaButtonReceiver;
import androidx.palette.graphics.Palette; import androidx.palette.graphics.Palette;
import com.bumptech.glide.Glide;
import com.fongmi.android.tv.App; import com.fongmi.android.tv.App;
import com.fongmi.android.tv.R; import com.fongmi.android.tv.R;
import com.fongmi.android.tv.event.ActionEvent; import com.fongmi.android.tv.event.ActionEvent;
import com.fongmi.android.tv.player.Players; import com.fongmi.android.tv.player.Players;
import com.fongmi.android.tv.receiver.ActionReceiver; import com.fongmi.android.tv.receiver.ActionReceiver;
import com.fongmi.android.tv.utils.ImgUtil;
import com.fongmi.android.tv.utils.Notify; import com.fongmi.android.tv.utils.Notify;
import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.EventBus;
@ -38,8 +36,6 @@ import java.util.Objects;
public class PlaybackService extends Service { public class PlaybackService extends Service {
private static final int MAX_CACHE_SIZE_BYTES = 10 * 1024 * 1024;
private LruCache<String, Bitmap> cache;
private static Players player; private static Players player;
public static void start(Players player) { public static void start(Players player) {
@ -84,8 +80,8 @@ public class PlaybackService extends Service {
return getMetadata() == null || getMetadata().getString(MediaMetadataCompat.METADATA_KEY_ARTIST).isEmpty() ? null : getMetadata().getString(MediaMetadataCompat.METADATA_KEY_ARTIST); return getMetadata() == null || getMetadata().getString(MediaMetadataCompat.METADATA_KEY_ARTIST).isEmpty() ? null : getMetadata().getString(MediaMetadataCompat.METADATA_KEY_ARTIST);
} }
private String getArtUri() { private Bitmap getArt() {
return getMetadata() == null ? "" : getMetadata().getString(MediaMetadataCompat.METADATA_KEY_ART_URI); return getMetadata() == null ? null : getMetadata().getBitmap(MediaMetadataCompat.METADATA_KEY_ART);
} }
private void addAction(NotificationCompat.Builder builder) { private void addAction(NotificationCompat.Builder builder) {
@ -106,38 +102,16 @@ public class PlaybackService extends Service {
builder.setDeleteIntent(ActionReceiver.getPendingIntent(this, ActionEvent.STOP)); builder.setDeleteIntent(ActionReceiver.getPendingIntent(this, ActionEvent.STOP));
if (nonNull()) builder.setContentIntent(player.getSession().getController().getSessionActivity()); if (nonNull()) builder.setContentIntent(player.getSession().getController().getSessionActivity());
if (nonNull()) builder.setStyle(new MediaStyle().setMediaSession(player.getSession().getSessionToken()).setShowActionsInCompactView(0, 1, 2)); if (nonNull()) builder.setStyle(new MediaStyle().setMediaSession(player.getSession().getSessionToken()).setShowActionsInCompactView(0, 1, 2));
if (getArt() != null) setIconColor(builder, getArt());
addAction(builder); addAction(builder);
setArtwork(builder);
return builder.build(); return builder.build();
} }
private void setArtwork(NotificationCompat.Builder builder) { private void setIconColor(NotificationCompat.Builder builder, Bitmap art) {
Bitmap bitmap = cache.get(getArtUri());
if (bitmap != null) {
setLargeIcon(builder, bitmap);
} else if (!getArtUri().isEmpty()) {
App.execute(() -> glide(builder));
}
}
private void glide(NotificationCompat.Builder builder) {
try {
Bitmap bitmap = Glide.with(this).asBitmap().load(ImgUtil.getUrl(getArtUri())).error(R.drawable.artwork).override(256, 256).submit().get();
cache.put(getArtUri(), bitmap);
setLargeIcon(builder, bitmap);
} catch (Exception e) {
e.printStackTrace();
}
}
private void setLargeIcon(NotificationCompat.Builder builder, Bitmap art) {
builder.setLargeIcon(art); builder.setLargeIcon(art);
Palette.from(art).generate(palette -> { Palette palette = Palette.from(art).generate();
int white = ContextCompat.getColor(this, R.color.white); int white = ContextCompat.getColor(this, R.color.white);
int color = palette != null ? palette.getMutedColor(palette.getVibrantColor(white)) : white; builder.setColor(palette.getMutedColor(palette.getVibrantColor(white)));
builder.setColor(color);
Notify.show(builder.build());
});
} }
@Subscribe(threadMode = ThreadMode.MAIN) @Subscribe(threadMode = ThreadMode.MAIN)
@ -149,15 +123,10 @@ public class PlaybackService extends Service {
public void onCreate() { public void onCreate() {
super.onCreate(); super.onCreate();
EventBus.getDefault().register(this); EventBus.getDefault().register(this);
cache = new LruCache<>(MAX_CACHE_SIZE_BYTES) {
@Override
protected int sizeOf(String key, Bitmap bitmap) {
return bitmap.getByteCount();
}
};
} }
@Override @Override
@SuppressLint("ForegroundServiceType")
public int onStartCommand(Intent intent, int flags, int startId) { public int onStartCommand(Intent intent, int flags, int startId) {
if (nonNull()) MediaButtonReceiver.handleIntent(player.getSession(), intent); if (nonNull()) MediaButtonReceiver.handleIntent(player.getSession(), intent);
int type = Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q ? ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK : 0; int type = Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q ? ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK : 0;

Loading…
Cancel
Save