gpt4 book ai didi

android - 如何在远程 View 中使用 Glide?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:02:01 24 4
gpt4 key购买 nike

我正在使用 Glide 从服务器加载所有图像,但我在尝试以正确的方式将它们设置为通知和 RemoteControlClientCompat(锁定屏幕时很酷的东西)时遇到故障。我正在开发一个音乐播放器,因此每次更改歌曲时,通知中的歌曲封面都必须更改。我有这段代码,它是第一次工作(尽管图像是从 url 或从 drawable 加载的),但在第二次调用时却没有。画质没变!更改歌曲时调用 CustomNotification。 RegisterRemoteClient 在启动 Activity 时被调用。

如果这不是正确的方法,请说明方法。

public void CustomNotification() {
Log.d(TAG, "Set custom notification");

simpleRemoteView = new RemoteViews(getApplicationContext().getPackageName(), R.layout.notification_custom);
expandedRemoteView = new RemoteViews(getApplicationContext().getPackageName(), R.layout.notification_big);

mNotification = new NotificationCompat.Builder(getApplicationContext())
.setSmallIcon(R.mipmap.ic_main_logo)
.setTicker(mSongTitle + " - " + mSongAuthors)
.setContentTitle(mSongTitle).build();

setRemoteListeners(simpleRemoteView);
setRemoteListeners(expandedRemoteView);

try {
//Check if playingSong has a cover url, if not set one from drawable
if(!playingSong.has("cover")){
mDummyAlbumArt = BitmapFactory.decodeResource(getResources(),R.drawable.image_song_album);
mDummyAlbumArt2 = BitmapFactory.decodeResource(getResources(),R.drawable.image_song_album);
mDummyAlbumArt3 = BitmapFactory.decodeResource(getResources(),R.drawable.image_song_album);
}else {
Glide.with(MainActivity.context)
.load(API.imgUrl(playingSong.getString("cover_img")))
.asBitmap()
.into(new SimpleTarget<Bitmap>(100, 100) {
@Override
public void onResourceReady(Bitmap bitmap, GlideAnimation anim) {
mDummyAlbumArt = bitmap;
mDummyAlbumArt2 = bitmap;
mDummyAlbumArt3 = bitmap;
}
});
}
} catch (JSONException e) {
e.printStackTrace();
}

mNotification.contentView = simpleRemoteView;
mNotification.contentView.setTextViewText(R.id.textSongName, mSongTitle);
mNotification.contentView.setTextViewText(R.id.textAlbumName, mSongAuthors);
mNotification.contentView.setImageViewBitmap(R.id.imageViewAlbumArt, mDummyAlbumArt);

if (currentVersionSupportBigNotification) {
mNotification.bigContentView = expandedRemoteView;
mNotification.bigContentView.setTextViewText(R.id.textSongName, mSongTitle);
mNotification.bigContentView.setTextViewText(R.id.textAlbumName, mSongAuthors);
mNotification.bigContentView.setImageViewBitmap(R.id.imageViewAlbumArt, mDummyAlbumArt2);
}

if (mPlayer != null) {
if (!mPlayer.isPlaying()) {
mNotification.contentView.setImageViewResource(R.id.btnPlayPause, R.mipmap.play);
if (currentVersionSupportBigNotification) {
mNotification.bigContentView.setImageViewResource(R.id.btnPlayPause, R.mipmap.play);
}
} else {
mNotification.contentView.setImageViewResource(R.id.btnPlayPause, R.mipmap.pause);
if (currentVersionSupportBigNotification) {
mNotification.bigContentView.setImageViewResource(R.id.btnPlayPause, R.mipmap.pause);
}
}
}

mNotification.flags |= Notification.FLAG_ONGOING_EVENT;
if(currentVersionSupportBigNotification) { //As priority_max only suported on API 16, the same as big notification
mNotification.priority = Notification.PRIORITY_MAX;
}
startForeground(NOTIFICATION_ID, mNotification);

//update remote controls
if (currentVersionSupportLockScreenControls) {
remoteControlClientCompat.editMetadata(true)
.putString(MediaMetadataRetriever.METADATA_KEY_TITLE, mSongTitle + " - " + mSongAuthors)
.putBitmap(RemoteControlClientCompat.MetadataEditorCompat.METADATA_KEY_ARTWORK, mDummyAlbumArt3)
.apply();
}
}

public void setRemoteListeners(RemoteViews remoteViews){
Log.d(TAG,"Setting remote listeners");
PendingIntent piAppActivity = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class),
PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent piPlayPause = PendingIntent.getService(this, 0, new Intent(MusicService.ACTION_TOGGLE_PLAYBACK), PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent piNext = PendingIntent.getService(this, 0, new Intent(MusicService.ACTION_SKIP), PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent piClose = PendingIntent.getService(this, 0, new Intent(MusicService.ACTION_STOP), PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent piPrevious = PendingIntent.getService(this, 0, new Intent(MusicService.ACTION_PREVIOUS), PendingIntent.FLAG_UPDATE_CURRENT);

remoteViews.setOnClickPendingIntent(R.id.linearLayoutNotification, piAppActivity);
remoteViews.setOnClickPendingIntent(R.id.btnPlayPause, piPlayPause);
remoteViews.setOnClickPendingIntent(R.id.btnNext, piNext);
remoteViews.setOnClickPendingIntent(R.id.btnDelete, piClose);
remoteViews.setOnClickPendingIntent(R.id.btnPrevious, piPrevious);
}

private void RegisterRemoteClient(){
// Use the media button APIs (if available) to register ourselves for media button
// events

MediaButtonHelper.registerMediaButtonEventReceiverCompat(mAudioManager, mMediaButtonReceiverComponent);
// Use the remote control APIs (if available) to set the playback state
if (remoteControlClientCompat == null) {
Intent intent = new Intent(Intent.ACTION_MEDIA_BUTTON);
intent.setComponent(mMediaButtonReceiverComponent);
remoteControlClientCompat = new RemoteControlClientCompat(PendingIntent.getBroadcast(this /*context*/,0 /*requestCode, ignored*/, intent /*intent*/, 0 /*flags*/));
RemoteControlHelper.registerRemoteControlClient(mAudioManager,remoteControlClientCompat);
}

remoteControlClientCompat.setPlaybackState(RemoteControlClient.PLAYSTATE_PLAYING);
remoteControlClientCompat.setTransportControlFlags(
RemoteControlClient.FLAG_KEY_MEDIA_PAUSE |
RemoteControlClient.FLAG_KEY_MEDIA_PREVIOUS |
RemoteControlClient.FLAG_KEY_MEDIA_NEXT |
RemoteControlClient.FLAG_KEY_MEDIA_STOP);
}

最佳答案

您需要设置使用 NotificationTarget 类将您的通知图像设置为滑动目标

NotificationTarget notificationTarget = new NotificationTarget(  
context,
R.id.iv_album_art,
remoteView,
notification,
NOTIFICATION_ID);

然后以通常的 Glide 方式使用该目标

    Uri uri = ContentUris.withAppendedId(PlayerConstants.sArtworkUri,
mediaitem.getAlbumId());

Glide.with(getApplicationContext())
.asBitmap()
.load(uri)
.into(notificationTarget);

这在 Glide 的指南中有解释

https://futurestud.io/blog/glide-loading-images-into-notifications-and-appwidgets

您可能还想制作专辑封面变化的动画 - 其描述如下:-

https://futurestud.io/blog/glide-custom-animations-with-animate

关于android - 如何在远程 View 中使用 Glide?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33220142/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com