Android MediaStyleNotification:播放/暂停按钮不响应单击

Android MediaStyleNotification:播放/暂停按钮不响应单击,android,notifications,android-notifications,mediabrowserservice,Android,Notifications,Android Notifications,Mediabrowserservice,在我的应用程序中,当用户选择要播放的音频文件时,当文件准备就绪并开始播放时,将成功显示MediaStyle通知 元数据信息已更新、标题、图标等,但 1.Play/Pause button does not respond to clicks. 2.Clicking the notification does not open the activity. 3.Swipe gesture triggers also… nothing 我不知道我错过了什么。你可以看看我的课程。正在讨论的代码:

在我的应用程序中,当用户选择要播放的音频文件时,当文件准备就绪并开始播放时,将成功显示MediaStyle通知

元数据信息已更新、标题、图标等,但

1.Play/Pause button does not respond to clicks.
2.Clicking the notification does not open the activity.
3.Swipe gesture triggers also… nothing


我不知道我错过了什么。你可以看看我的课程。

正在讨论的代码:

 private void showMediaStyleNotification() {
        // Given a media session and its context (usually the component containing the session)
        // Create a NotificationCompat.Builder

        // Get the session's metadata
        MediaControllerCompat controller = session.getController();
        MediaMetadataCompat mediaMetadata = controller.getMetadata();
        MediaDescriptionCompat description = mediaMetadata.getDescription();

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);

        // Add the metadata for the currently playing track
        builder
                .setContentTitle(description.getTitle())
                .setContentText(description.getSubtitle())
                .setSubText(description.getDescription())
                .setLargeIcon(description.getIconBitmap())

                // Enable launching the player by clicking the notification
                .setContentIntent(controller.getSessionActivity())

                // Stop the service when the notification is swiped away
                .setDeleteIntent(MediaButtonReceiver.buildMediaButtonPendingIntent(this,
                        PlaybackStateCompat.ACTION_STOP))

                // Make the transport controls visible on the lockscreen
                .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)

                // Add an app icon and set its accent color
                // Be careful about the color
                .setSmallIcon(R.drawable.ic_headset_pink_40dp)
                .setColor(ContextCompat.getColor(this, R.color.secondaryColor))

                // Add a pause button
                .addAction(new NotificationCompat.Action(
                        R.drawable.ic_pause_white_40dp, getString(R.string.pause_label),
                        MediaButtonReceiver.buildMediaButtonPendingIntent(this,
                                PlaybackStateCompat.ACTION_PLAY_PAUSE)))

                // Take advantage of MediaStyle features
                .setStyle(new MediaStyle()
                        .setMediaSession(session.getSessionToken())
                        .setShowActionsInCompactView(0)

                        // Add a cancel button
                        .setShowCancelButton(true)
                        .setCancelButtonIntent(MediaButtonReceiver.buildMediaButtonPendingIntent(this,
                                PlaybackStateCompat.ACTION_STOP)));

// Display the notification and place the service in the foreground
        startForeground(FOREGROUND_ID, builder.build());
    }
}
初始化分离:

stateBuilder = new PlaybackStateCompat.Builder()
                .setActions(PlaybackStateCompat.ACTION_PLAY |
                        PlaybackStateCompat.ACTION_PLAY_PAUSE);
        session.setPlaybackState(stateBuilder.build());
当我按play/pause时,它进入这里:GetPlayBackstate fromPlayerState。唯一的问题是播放/暂停按钮图形没有更新。它总是显示暂停

   case STARTED:
        return stateBuilder.setState(PlaybackStateCompat.STATE_PLAYING, currentPosition, playbackSpeed)
                .setActions(PlaybackStateCompat.ACTION_PAUSE | PlaybackStateCompat.ACTION_PLAY_PAUSE
                        | PlaybackStateCompat.ACTION_STOP | PlaybackStateCompat.ACTION_SEEK_TO
                        | PlaybackStateCompat.ACTION_PLAY)
                .build();
    case PAUSED:
        return stateBuilder.setState(PlaybackStateCompat.STATE_PAUSED, currentPosition, playbackSpeed)
                .setActions(PlaybackStateCompat.ACTION_PLAY | PlaybackStateCompat.ACTION_PLAY_PAUSE
                        | PlaybackStateCompat.ACTION_STOP | PlaybackStateCompat.ACTION_SEEK_TO
                        | PlaybackStateCompat.ACTION_PAUSE)
                .build();

快速查看一下,您的类似乎没有遵循与相同的布局。还有一个解释如何从通知中打开意图的方法。你是对的@WoodyDev,它没有遵循你建议的布局。但我相信它确实遵循了官方文档->中的布局。您在最新版本的代码中尝试过这个吗?通知中的播放/暂停按钮对我来说工作正常-在安卓5.1手机上测试。[在安卓8.0上,由于某种原因,通知不会出现]我想您可能在使用主屏幕小部件时已经修复了它。就未决的意图处理而言,它们共享大部分相同的框架,这很好。