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

问题描述 投票:0回答:1

我编写了一个演示文件来播放音乐,并希望使用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()
            );
        }
    }
android android-notifications
1个回答
0
投票

首先:建立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)
                );
© www.soinside.com 2019 - 2024. All rights reserved.