Reputation: 1010
I'm developing a media player. Unfortunately I can't use the MediaBrowserService to better support devices connected over bluetooth. But at least I implemented the MediaSession class to broadcast playback information using the AVRCP protocol.
When I start playback, my Wear watch automatically shows a notification but only with Play/Pause button. If I use Previous/Next buttons on my headphones or in my car, it works perfectly. But I don't have those buttons in the notification on Android Wear.
Is it possible to force the watch to display those buttons? If I sart any other player, I have those buttons on my watch. But not with my own player.
Here is my code:
mMediaSession = new MediaSessionCompat(this, "MuzikaServiceMediaSession");
mMediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
mMediaSession.setActive(true);
mMediaSession.setCallback(myMediaSessionCallback);
PlaybackStateCompat state = new PlaybackStateCompat.Builder()
.setActions(PlaybackStateCompat.ACTION_PLAY_PAUSE | PlaybackStateCompat.ACTION_PLAY | PlaybackStateCompat.ACTION_PAUSE |
PlaybackStateCompat.ACTION_SKIP_TO_NEXT | PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS |
PlaybackStateCompat.ACTION_FAST_FORWARD | PlaybackStateCompat.ACTION_REWIND
)
.setState(PlaybackStateCompat.STATE_STOPPED, 0, 1f)
.build();
mMediaSession.setPlaybackState(state);
Thank you.
Upvotes: 1
Views: 849
Reputation: 173
I had similar problem. Reason was using exoplayer's MediaSessionConnector which override media buttons functions. By default MediaSessionConnector override only PLAY/PAUSE button and when you try to use PlaybackStateCompat.Builder() it not work because MediaSessionConnector override this job. MediaSessionConnector not incude ACTION_SKIP_TO_PREVIOUS and ACTION_SKIP_TO_NEXT becuse for this you need to implement TimelineQueueNavigator (It handles the actions ACTION_SKIP_PREVIOUS, ACTION_SKIP_NEXT and ACTION_SKIP_TO_QUEUE_ITEM) as part of MediaSessionConnector. See details at the Medium page
For fixing it I removed totally MediaSessionConnector from my code and add next code at initialization:
PlaybackStateCompat.Builder stateBuilder;
stateBuilder = new PlaybackStateCompat.Builder();
stateBuilder.setActions(PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS | PlaybackStateCompat.ACTION_PLAY_PAUSE|PlaybackStateCompat.ACTION_SKIP_TO_NEXT);
and this part to method which update song metadata and notification:
int state;
if (isPlaying) {
state = 3;
} else {
state = 2;
}
stateBuilder.setState(state, 0, 1.0f);
mediaSession.setPlaybackState(stateBuilder.build());
Also there is another solution to create MediaSessionConnector with correct TimelineQueueNavigator and queue with your songs. In this case buttons will appear at wear os, I checked it at my watch.
Upvotes: 2