如何在remoteViews中使用Glide? [英] How to use Glide in remoteViews?

查看:82
本文介绍了如何在remoteViews中使用Glide?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Glide从服务器加载所有图像,但是我在尝试以正确的方式将它们设置为通知和RemoteControlClientCompat(具有锁定屏幕的超酷功能)时遇到了疑难解答.我正在开发音乐播放器,因此每次更改歌曲时,通知中的歌曲封面都必须更改.我有此代码,并且它是第一次工作(虽然图像是从url或可绘制对象加载的),但第二次调用时却没有.图像不变!更改歌曲时将调用CustomNotification.然后在启动活动时调用RegisterRemoteClient.

I'm using Glide for all loading images from server, but I'm having troubleshooting trying to set them in a correct way to notifications and RemoteControlClientCompat (that cool thing with lock screens). I am developing a music player, so every time that a song is changed the song cover from notifications has to change. I have this code and it works for the first time (althoug the image is load from url or from drawable), but not when it is called for second time. The image doesn't change! CustomNotification is invoked when a song is changed. And RegisterRemoteClient is invoked on start activity.

如果这不是正确的方法,请说明如何做.

If this is not the correct way to do it, please tell how.

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);
}

推荐答案

您需要设置使用

You need to set use NotificationTarget class to set your notification image as glide target

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

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

and then use that target in usual glide way

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

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

在此处的Glide指南中对此进行了解释

It is explained in Glide's guideline here

https://futurestud.io/blog/glide-将图像加载到通知和附件中

您可能还希望为专辑封面更改制作动画-其描述如下:-

You might also like to animate album art change- Its described here:-

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

这篇关于如何在remoteViews中使用Glide?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆