Notification.MediaStyle 如何显示媒体播放器播放的进度?

How does Notification.MediaStyle show progress which mediaplayer playback?

我写了一个播放音乐的demo,想用Notification.MediaStyle()来显示通知,进度条显示出来了,但是没用。

什么方法我都试了一遍又一遍,都没有用。 这是我的代码:

    private void notifyManager() {
        ComponentName componentName = new ComponentName(getPackageName(), 
        CustomBroadcast.class.getSimpleName());
        Intent playButtonIntent = new Intent();
        playButtonIntent.setComponent(componentName);
        playButtonIntent.setAction("caacle_end");
        playButtonIntent.putExtra("isPlaying", isPlaying);

        PendingIntent playIntent = PendingIntent.getBroadcast(this,
                (int) (System.currentTimeMillis() / 10000),
                playButtonIntent, PendingIntent.FLAG_CANCEL_CURRENT);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            startForeground(100,
                    new Notification.Builder(this, "c1111")
                            .setContentTitle("Test")
                            .setContentText("hhhh")
                            .setSmallIcon(R.mipmap.ic_launcher_round)
                            .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.bg_3))
                            .setSubText("gggg")
                            .setColorized(true)
                            .addAction(new Notification.Action.Builder(R.mipmap.voice_on, "play", null).build())
                            .addAction(new Notification.Action.Builder(isPlaying ? R.mipmap.zanting_2 : R.mipmap.bofang_2, "pause", playIntent).build())
                            .addAction(new Notification.Action.Builder(R.mipmap.zhendong, "hf", null).build())
                            .setStyle(new Notification.MediaStyle().setMediaSession(mSession.getSessionToken()).setShowActionsInCompactView(0, 1, 2))
                            .setCategory(Notification.CATEGORY_TRANSPORT)
                            .build()
            );
        }
    }

首先:构建 MediaMetadata

    MediaMetadataCompat.Builder mediaMetaData_builder = newMediaMetadataCompat.Builder();    

mediaMetaData_builder.putLong(MediaMetadataCompat.METADATA_KEY_DURATION,getYourSong().getDuration() );

其次:将 MediaMetadata 设置为 MediaSession mMediaSession.setMetadata(mediaMetaData_builder.build());

第三:构建PlayBackState

 mPlaybackState = new PlaybackStateCompat.Builder().
            setState(mPlaybackState.getState(), YourSong.getCurrent(), 1.0f)
            .setActions( PlaybackStateCompat.ACTION_PLAY |
                    PlaybackStateCompat.ACTION_PAUSE |
                    PlaybackStateCompat.ACTION_SKIP_TO_NEXT |
                    PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS |
                    PlaybackStateCompat.ACTION_SEEK_TO )
            .build();
mMediaSession.setPlaybackState(mPlaybackState);

第四:用MediaSession设置通知

builder = new NotificationCompat.Builder(mContext, NOTIFICATION_CHANNEL_ID)
.setMediaSession(mMediaSession.getSessionToken()).setShowActionsInCompactView(0,1,2)
                        .setShowCancelButton(true)
                );