Android媒体通知的大图标未更改。

huangapple go评论79阅读模式
英文:

Android media notification large icon doesn't change

问题

I have a media notification and a large icon, my code successfully loads the image the first time, but the image doesn't change the second time. My code was working on Android 8.1.0, but currently, I'm using Android 12, and it doesn't work.

I tried removing MediaStyle, and it worked. Images successfully changed each time, but I need to use MediaStyle. I'm sure about updating the notification, title, and song name changes, but the image doesn't.

英文:

I have a media notification and a large icon, my code successfully loads the image first time, but image doesn't changes second time. My code was working in Android 8.1.0, currently I'm using Android 12 and it doesn't work.

    mediaSession.setMetadata(new MediaMetadataCompat.Builder()
            .putBitmap(MediaMetadata.METADATA_KEY_ALBUM_ART, image)
            .putBitmap(MediaMetadata.METADATA_KEY_ART, image)
            .putLong(MediaMetadata.METADATA_KEY_DURATION, -1L)
            .putString(MediaMetadata.METADATA_KEY_TITLE,BASLIK)
            .putString(MediaMetadata.METADATA_KEY_ARTIST,ISIM)
            .build());

    stateBuilder = new PlaybackStateCompat.Builder();
    stateBuilder.setActions(PlaybackStateCompat.ACTION_PLAY_PAUSE | PlaybackStateCompat.ACT
    stateBuilder.setState(PlaybackStateCompat.STATE_PLAYING, 0, 1.0f);
    mediaSession.setPlaybackState(stateBuilder.build());

    notification = new NotificationCompat.Builder(context, CHANNEL_ID)
            .setContentTitle(BASLIK)
            .setContentText(ISIM)
            .setLargeIcon(image)
            .setSmallIcon(R.drawable.ic_launcher_foreground)
            .addAction(R.drawable.ic_previous, "Previous", prevPendingIntent)
            .addAction(icon, "Play", playPendingIntent)
            .addAction(R.drawable.ic_next, "Next", nextPendingIntent)
            .setOnlyAlertOnce(true)
            .setPriority(Notification.PRIORITY_LOW)
            .setOngoing(true)
            .setShowWhen(false)
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
            .setStyle(new androidx.media.app.NotificationCompat.MediaStyle()
                    .setShowActionsInCompactView(0, 1, 2)
                    .setMediaSession(mediaSession.getSessionToken()));
            
    notificationManager.notify(Integer.parseInt(CHANNEL_ID), notification.build());

I tried removing MediaStyle and it worked, images successfully changed each time but I need to use MediaStyle. I'm sure about updating the notification, title and song name changes but image doesn't.

答案1

得分: 0

我通过添加 metadata.putLong(MediaMetadata.METADATA_KEY_DURATION, duration); 解决了我的问题。duration 变量应该是 long 类型,并且应该始终更新。

英文:

I solved my problem by adding metadata.putLong(MediaMetadata.METADATA_KEY_DURATION, duration);. duration variable should be long and it should always get updated.

huangapple
  • 本文由 发表于 2023年3月31日 03:59:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/75892486.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定