Skip to content

Commit

Permalink
let apps intercept/handle media button events by using a MediaButtonE…
Browse files Browse the repository at this point in the history
…ventHandler

Issue #5179

PiperOrigin-RevId: 234571837
  • Loading branch information
marcbaechinger authored and ojw28 committed Feb 19, 2019
1 parent 0622afe commit 44e23fa
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
2 changes: 2 additions & 0 deletions RELEASENOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@
`DownloadNotificationHelper`.
* Move creation of dialogs for `TrackSelectionView`s to
`TrackSelectionDialogBuilder` and add option to select multiple overrides.
* MediaSessionConnector: Let apps intercept media button events
([#5179](https:/google/ExoPlayer/issues/5179)).

### 2.9.5 ###

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.google.android.exoplayer2.ext.mediasession;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
Expand Down Expand Up @@ -76,6 +77,9 @@
* is recommended for most use cases.
* <li>To enable editing of the media queue, you can set a {@link QueueEditor} by calling {@link
* #setQueueEditor(QueueEditor)}.
* <li>A {@link MediaButtonEventHandler} can be set by calling {@link
* #setMediaButtonEventHandler(MediaButtonEventHandler)}. By default media button events are
* handled by {@link MediaSessionCompat.Callback#onMediaButtonEvent(Intent)}.
* <li>An {@link ErrorMessageProvider} for providing human readable error messages and
* corresponding error codes can be set by calling {@link
* #setErrorMessageProvider(ErrorMessageProvider)}.
Expand Down Expand Up @@ -300,6 +304,21 @@ public interface RatingCallback extends CommandReceiver {
void onSetRating(Player player, RatingCompat rating, Bundle extras);
}

/** Handles a media button event. */
public interface MediaButtonEventHandler {
/**
* See {@link MediaSessionCompat.Callback#onMediaButtonEvent(Intent)}.
*
* @param player The {@link Player}.
* @param controlDispatcher A {@link ControlDispatcher} that should be used for dispatching
* changes to the player.
* @param mediaButtonEvent The {@link Intent}.
* @return True if the event was handled, false otherwise.
*/
boolean onMediaButtonEvent(
Player player, ControlDispatcher controlDispatcher, Intent mediaButtonEvent);
}

/**
* Provides a {@link PlaybackStateCompat.CustomAction} to be published and handles the action when
* sent by a media controller.
Expand Down Expand Up @@ -357,6 +376,7 @@ public interface MediaMetadataProvider {
@Nullable private QueueNavigator queueNavigator;
@Nullable private QueueEditor queueEditor;
@Nullable private RatingCallback ratingCallback;
@Nullable private MediaButtonEventHandler mediaButtonEventHandler;

private long enabledPlaybackActions;
private int rewindMs;
Expand Down Expand Up @@ -432,6 +452,18 @@ public void setControlDispatcher(@Nullable ControlDispatcher controlDispatcher)
}
}

/**
* Sets the {@link MediaButtonEventHandler}. Pass {@code null} if the media button event should be
* handled by {@link MediaSessionCompat.Callback#onMediaButtonEvent(Intent)}.
*
* @param mediaButtonEventHandler The {@link MediaButtonEventHandler}, or null to let the event be
* handled by {@link MediaSessionCompat.Callback#onMediaButtonEvent(Intent)}.
*/
public void setMediaButtonEventHandler(
@Nullable MediaButtonEventHandler mediaButtonEventHandler) {
this.mediaButtonEventHandler = mediaButtonEventHandler;
}

/**
* Sets the enabled playback actions.
*
Expand Down Expand Up @@ -753,6 +785,10 @@ private boolean canDispatchQueueEdit() {
return player != null && queueEditor != null;
}

private boolean canDispatchMediaButtonEvent() {
return player != null && mediaButtonEventHandler != null;
}

private void stopPlayerForPrepare(boolean playWhenReady) {
if (player != null) {
player.stop();
Expand Down Expand Up @@ -1169,5 +1205,14 @@ public void onRemoveQueueItem(MediaDescriptionCompat description) {
queueEditor.onRemoveQueueItem(player, description);
}
}

@Override
public boolean onMediaButtonEvent(Intent mediaButtonEvent) {
boolean isHandled =
canDispatchMediaButtonEvent()
&& mediaButtonEventHandler.onMediaButtonEvent(
player, controlDispatcher, mediaButtonEvent);
return isHandled || super.onMediaButtonEvent(mediaButtonEvent);
}
}
}

0 comments on commit 44e23fa

Please sign in to comment.