MediaStyleNotification: кнопка «Воспроизведение / Пауза» не реагирует на нажатие - PullRequest
0 голосов
/ 25 июня 2018

В моем приложении, когда пользователь выбирает аудиофайл для воспроизведения, когда файл готов и воспроизведение начинается, уведомление 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

enter image description here

Я не знаю, что мне не хватает.Вы можете взглянуть на мой MediaBrowserService класс.

1 Ответ

0 голосов
/ 19 июля 2018

обсуждаемый код:

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

initializeMediaSession:

stateBuilder = new PlaybackStateCompat.Builder()
                .setActions(PlaybackStateCompat.ACTION_PLAY |
                        PlaybackStateCompat.ACTION_PLAY_PAUSE);
        session.setPlaybackState(stateBuilder.build());

Когда я нажимаю кнопку воспроизведения / паузы, происходит следующее: getPlaybackStateFromPlayerState. Единственная проблема в том, что графика кнопки воспроизведения / паузы не обновляется. Это всегда показывает паузу ...

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

enter image description here

...