RemotePlaybackClientpublic class RemotePlaybackClient extends Object A helper class for playing media on remote routes using the remote playback protocol
defined by {@link MediaControlIntent}.
The client maintains session state and offers a simplified interface for issuing
remote playback media control intents to a single route.
|
Fields Summary |
---|
private static final String | TAG | private static final boolean | DEBUG | private final android.content.Context | mContext | private final MediaRouter.RouteInfo | mRoute | private final StatusReceiver | mStatusReceiver | private final android.app.PendingIntent | mItemStatusPendingIntent | private final android.app.PendingIntent | mSessionStatusPendingIntent | private boolean | mRouteSupportsRemotePlayback | private boolean | mRouteSupportsQueuing | private boolean | mRouteSupportsSessionManagement | private String | mSessionId | private StatusCallback | mStatusCallback |
Constructors Summary |
---|
public RemotePlaybackClient(android.content.Context context, MediaRouter.RouteInfo route)Creates a remote playback client for a route.
if (context == null) {
throw new IllegalArgumentException("context must not be null");
}
if (route == null) {
throw new IllegalArgumentException("route must not be null");
}
mContext = context;
mRoute = route;
IntentFilter statusFilter = new IntentFilter();
statusFilter.addAction(StatusReceiver.ACTION_ITEM_STATUS_CHANGED);
statusFilter.addAction(StatusReceiver.ACTION_SESSION_STATUS_CHANGED);
mStatusReceiver = new StatusReceiver();
context.registerReceiver(mStatusReceiver, statusFilter);
Intent itemStatusIntent = new Intent(StatusReceiver.ACTION_ITEM_STATUS_CHANGED);
itemStatusIntent.setPackage(context.getPackageName());
mItemStatusPendingIntent = PendingIntent.getBroadcast(
context, 0, itemStatusIntent, 0);
Intent sessionStatusIntent = new Intent(StatusReceiver.ACTION_SESSION_STATUS_CHANGED);
sessionStatusIntent.setPackage(context.getPackageName());
mSessionStatusPendingIntent = PendingIntent.getBroadcast(
context, 0, sessionStatusIntent, 0);
detectFeatures();
|
Methods Summary |
---|
private void | adoptSession(java.lang.String sessionId)
if (sessionId != null) {
setSessionId(sessionId);
}
| private static java.lang.String | bundleToString(android.os.Bundle bundle)
if (bundle != null) {
bundle.size(); // force bundle to be unparcelled
return bundle.toString();
}
return "null";
| private void | detectFeatures()
mRouteSupportsRemotePlayback = routeSupportsAction(MediaControlIntent.ACTION_PLAY)
&& routeSupportsAction(MediaControlIntent.ACTION_SEEK)
&& routeSupportsAction(MediaControlIntent.ACTION_GET_STATUS)
&& routeSupportsAction(MediaControlIntent.ACTION_PAUSE)
&& routeSupportsAction(MediaControlIntent.ACTION_RESUME)
&& routeSupportsAction(MediaControlIntent.ACTION_STOP);
mRouteSupportsQueuing = mRouteSupportsRemotePlayback
&& routeSupportsAction(MediaControlIntent.ACTION_ENQUEUE)
&& routeSupportsAction(MediaControlIntent.ACTION_REMOVE);
mRouteSupportsSessionManagement = mRouteSupportsRemotePlayback
&& routeSupportsAction(MediaControlIntent.ACTION_START_SESSION)
&& routeSupportsAction(MediaControlIntent.ACTION_GET_SESSION_STATUS)
&& routeSupportsAction(MediaControlIntent.ACTION_END_SESSION);
| public void | endSession(android.os.Bundle extras, android.support.v7.media.RemotePlaybackClient$SessionActionCallback callback)Sends a request to end the media playback session.
The request is issued in the current session. If this request is successful,
the {@link #getSessionId session id property} will be set to null after
the callback is invoked.
Please refer to {@link MediaControlIntent#ACTION_END_SESSION ACTION_END_SESSION}
for more information about the semantics of this request.
throwIfSessionManagementNotSupported();
throwIfNoCurrentSession();
Intent intent = new Intent(MediaControlIntent.ACTION_END_SESSION);
performSessionAction(intent, mSessionId, extras, callback);
| public void | enqueue(android.net.Uri contentUri, java.lang.String mimeType, android.os.Bundle metadata, long positionMillis, android.os.Bundle extras, android.support.v7.media.RemotePlaybackClient$ItemActionCallback callback)Sends a request to enqueue a media item.
Enqueues a new item to play. If the queue was previously paused, then will
remain paused.
The request is issued in the current session. If no session is available, then
one is created implicitly.
Please refer to {@link MediaControlIntent#ACTION_ENQUEUE ACTION_ENQUEUE} for
more information about the semantics of this request.
playOrEnqueue(contentUri, mimeType, metadata, positionMillis,
extras, callback, MediaControlIntent.ACTION_ENQUEUE);
| public java.lang.String | getSessionId()Gets the current session id if there is one.
return mSessionId;
| public void | getSessionStatus(android.os.Bundle extras, android.support.v7.media.RemotePlaybackClient$SessionActionCallback callback)Sends a request to get the status of the media playback session.
The request is issued in the current session.
Please refer to {@link MediaControlIntent#ACTION_GET_SESSION_STATUS
ACTION_GET_SESSION_STATUS} for more information about the semantics of this request.
throwIfSessionManagementNotSupported();
throwIfNoCurrentSession();
Intent intent = new Intent(MediaControlIntent.ACTION_GET_SESSION_STATUS);
performSessionAction(intent, mSessionId, extras, callback);
| public void | getStatus(java.lang.String itemId, android.os.Bundle extras, android.support.v7.media.RemotePlaybackClient$ItemActionCallback callback)Sends a request to get the status of a media item.
The request is issued in the current session.
Please refer to {@link MediaControlIntent#ACTION_GET_STATUS ACTION_GET_STATUS} for
more information about the semantics of this request.
if (itemId == null) {
throw new IllegalArgumentException("itemId must not be null");
}
throwIfNoCurrentSession();
Intent intent = new Intent(MediaControlIntent.ACTION_GET_STATUS);
performItemAction(intent, mSessionId, itemId, extras, callback);
| private void | handleError(android.content.Intent intent, android.support.v7.media.RemotePlaybackClient$ActionCallback callback, java.lang.String error, android.os.Bundle data)
final int code;
if (data != null) {
code = data.getInt(MediaControlIntent.EXTRA_ERROR_CODE,
MediaControlIntent.ERROR_UNKNOWN);
} else {
code = MediaControlIntent.ERROR_UNKNOWN;
}
if (DEBUG) {
Log.w(TAG, "Received error from " + intent.getAction()
+ ": error=" + error
+ ", code=" + code
+ ", data=" + bundleToString(data));
}
callback.onError(error, code, data);
| private void | handleInvalidResult(android.content.Intent intent, android.support.v7.media.RemotePlaybackClient$ActionCallback callback, android.os.Bundle data)
Log.w(TAG, "Received invalid result data from " + intent.getAction()
+ ": data=" + bundleToString(data));
callback.onError(null, MediaControlIntent.ERROR_UNKNOWN, data);
| public boolean | hasSession()Returns true if the client currently has a session.
Equivalent to checking whether {@link #getSessionId} returns a non-null result.
return mSessionId != null;
| private static java.lang.String | inferMissingResult(java.lang.String request, java.lang.String result)
if (result == null) {
// Result is missing.
return request;
}
if (request == null || request.equals(result)) {
// Request didn't specify a value or result matches request.
return result;
}
// Result conflicts with request.
return null;
| public boolean | isQueuingSupported()Returns true if the route supports queuing features.
If the route does not support queuing, then at most one media item can be played
at a time and the {@link #enqueue} method will not be available.
This method returns true if the route supports all of the basic remote playback
actions and all of the following actions:
{@link MediaControlIntent#ACTION_ENQUEUE enqueue},
{@link MediaControlIntent#ACTION_REMOVE remove}.
return mRouteSupportsQueuing;
| public boolean | isRemotePlaybackSupported()Returns true if the route supports remote playback.
If the route does not support remote playback, then none of the functionality
offered by the client will be available.
This method returns true if the route supports all of the following
actions: {@link MediaControlIntent#ACTION_PLAY play},
{@link MediaControlIntent#ACTION_SEEK seek},
{@link MediaControlIntent#ACTION_GET_STATUS get status},
{@link MediaControlIntent#ACTION_PAUSE pause},
{@link MediaControlIntent#ACTION_RESUME resume},
{@link MediaControlIntent#ACTION_STOP stop}.
return mRouteSupportsRemotePlayback;
| public boolean | isSessionManagementSupported()Returns true if the route supports session management features.
If the route does not support session management, then the session will
not be created until the first media item is played.
This method returns true if the route supports all of the basic remote playback
actions and all of the following actions:
{@link MediaControlIntent#ACTION_START_SESSION start session},
{@link MediaControlIntent#ACTION_GET_SESSION_STATUS get session status},
{@link MediaControlIntent#ACTION_END_SESSION end session}.
return mRouteSupportsSessionManagement;
| private static void | logRequest(android.content.Intent intent)
if (DEBUG) {
Log.d(TAG, "Sending request: " + intent);
}
| public void | pause(android.os.Bundle extras, android.support.v7.media.RemotePlaybackClient$SessionActionCallback callback)Sends a request to pause media playback.
The request is issued in the current session. If playback is already paused
then the request has no effect.
Please refer to {@link MediaControlIntent#ACTION_PAUSE ACTION_PAUSE} for
more information about the semantics of this request.
throwIfNoCurrentSession();
Intent intent = new Intent(MediaControlIntent.ACTION_PAUSE);
performSessionAction(intent, mSessionId, extras, callback);
| private void | performItemAction(android.content.Intent intent, java.lang.String sessionId, java.lang.String itemId, android.os.Bundle extras, android.support.v7.media.RemotePlaybackClient$ItemActionCallback callback)
intent.addCategory(MediaControlIntent.CATEGORY_REMOTE_PLAYBACK);
if (sessionId != null) {
intent.putExtra(MediaControlIntent.EXTRA_SESSION_ID, sessionId);
}
if (itemId != null) {
intent.putExtra(MediaControlIntent.EXTRA_ITEM_ID, itemId);
}
if (extras != null) {
intent.putExtras(extras);
}
logRequest(intent);
mRoute.sendControlRequest(intent, new MediaRouter.ControlRequestCallback() {
@Override
public void onResult(Bundle data) {
if (data != null) {
String sessionIdResult = inferMissingResult(sessionId,
data.getString(MediaControlIntent.EXTRA_SESSION_ID));
MediaSessionStatus sessionStatus = MediaSessionStatus.fromBundle(
data.getBundle(MediaControlIntent.EXTRA_SESSION_STATUS));
String itemIdResult = inferMissingResult(itemId,
data.getString(MediaControlIntent.EXTRA_ITEM_ID));
MediaItemStatus itemStatus = MediaItemStatus.fromBundle(
data.getBundle(MediaControlIntent.EXTRA_ITEM_STATUS));
adoptSession(sessionIdResult);
if (sessionIdResult != null && itemIdResult != null && itemStatus != null) {
if (DEBUG) {
Log.d(TAG, "Received result from " + intent.getAction()
+ ": data=" + bundleToString(data)
+ ", sessionId=" + sessionIdResult
+ ", sessionStatus=" + sessionStatus
+ ", itemId=" + itemIdResult
+ ", itemStatus=" + itemStatus);
}
callback.onResult(data, sessionIdResult, sessionStatus,
itemIdResult, itemStatus);
return;
}
}
handleInvalidResult(intent, callback, data);
}
@Override
public void onError(String error, Bundle data) {
handleError(intent, callback, error, data);
}
});
| private void | performSessionAction(android.content.Intent intent, java.lang.String sessionId, android.os.Bundle extras, android.support.v7.media.RemotePlaybackClient$SessionActionCallback callback)
intent.addCategory(MediaControlIntent.CATEGORY_REMOTE_PLAYBACK);
if (sessionId != null) {
intent.putExtra(MediaControlIntent.EXTRA_SESSION_ID, sessionId);
}
if (extras != null) {
intent.putExtras(extras);
}
logRequest(intent);
mRoute.sendControlRequest(intent, new MediaRouter.ControlRequestCallback() {
@Override
public void onResult(Bundle data) {
if (data != null) {
String sessionIdResult = inferMissingResult(sessionId,
data.getString(MediaControlIntent.EXTRA_SESSION_ID));
MediaSessionStatus sessionStatus = MediaSessionStatus.fromBundle(
data.getBundle(MediaControlIntent.EXTRA_SESSION_STATUS));
adoptSession(sessionIdResult);
if (sessionIdResult != null) {
if (DEBUG) {
Log.d(TAG, "Received result from " + intent.getAction()
+ ": data=" + bundleToString(data)
+ ", sessionId=" + sessionIdResult
+ ", sessionStatus=" + sessionStatus);
}
try {
callback.onResult(data, sessionIdResult, sessionStatus);
} finally {
if (intent.getAction().equals(MediaControlIntent.ACTION_END_SESSION)
&& sessionIdResult.equals(mSessionId)) {
setSessionId(null);
}
}
return;
}
}
handleInvalidResult(intent, callback, data);
}
@Override
public void onError(String error, Bundle data) {
handleError(intent, callback, error, data);
}
});
| public void | play(android.net.Uri contentUri, java.lang.String mimeType, android.os.Bundle metadata, long positionMillis, android.os.Bundle extras, android.support.v7.media.RemotePlaybackClient$ItemActionCallback callback)Sends a request to play a media item.
Clears the queue and starts playing the new item immediately. If the queue
was previously paused, then it is resumed as a side-effect of this request.
The request is issued in the current session. If no session is available, then
one is created implicitly.
Please refer to {@link MediaControlIntent#ACTION_PLAY ACTION_PLAY} for
more information about the semantics of this request.
playOrEnqueue(contentUri, mimeType, metadata, positionMillis,
extras, callback, MediaControlIntent.ACTION_PLAY);
| private void | playOrEnqueue(android.net.Uri contentUri, java.lang.String mimeType, android.os.Bundle metadata, long positionMillis, android.os.Bundle extras, android.support.v7.media.RemotePlaybackClient$ItemActionCallback callback, java.lang.String action)
if (contentUri == null) {
throw new IllegalArgumentException("contentUri must not be null");
}
throwIfRemotePlaybackNotSupported();
if (action.equals(MediaControlIntent.ACTION_ENQUEUE)) {
throwIfQueuingNotSupported();
}
Intent intent = new Intent(action);
intent.setDataAndType(contentUri, mimeType);
intent.putExtra(MediaControlIntent.EXTRA_ITEM_STATUS_UPDATE_RECEIVER,
mItemStatusPendingIntent);
if (metadata != null) {
intent.putExtra(MediaControlIntent.EXTRA_ITEM_METADATA, metadata);
}
if (positionMillis != 0) {
intent.putExtra(MediaControlIntent.EXTRA_ITEM_CONTENT_POSITION, positionMillis);
}
performItemAction(intent, mSessionId, null, extras, callback);
| public void | release()Releases resources owned by the client.
mContext.unregisterReceiver(mStatusReceiver);
| public void | remove(java.lang.String itemId, android.os.Bundle extras, android.support.v7.media.RemotePlaybackClient$ItemActionCallback callback)Sends a request to remove a media item from the queue.
The request is issued in the current session.
Please refer to {@link MediaControlIntent#ACTION_REMOVE ACTION_REMOVE} for
more information about the semantics of this request.
if (itemId == null) {
throw new IllegalArgumentException("itemId must not be null");
}
throwIfQueuingNotSupported();
throwIfNoCurrentSession();
Intent intent = new Intent(MediaControlIntent.ACTION_REMOVE);
performItemAction(intent, mSessionId, itemId, extras, callback);
| public void | resume(android.os.Bundle extras, android.support.v7.media.RemotePlaybackClient$SessionActionCallback callback)Sends a request to resume (unpause) media playback.
The request is issued in the current session. If playback is not paused
then the request has no effect.
Please refer to {@link MediaControlIntent#ACTION_RESUME ACTION_RESUME} for
more information about the semantics of this request.
throwIfNoCurrentSession();
Intent intent = new Intent(MediaControlIntent.ACTION_RESUME);
performSessionAction(intent, mSessionId, extras, callback);
| private boolean | routeSupportsAction(java.lang.String action)
return mRoute.supportsControlAction(MediaControlIntent.CATEGORY_REMOTE_PLAYBACK, action);
| public void | seek(java.lang.String itemId, long positionMillis, android.os.Bundle extras, android.support.v7.media.RemotePlaybackClient$ItemActionCallback callback)Sends a request to seek to a new position in a media item.
Seeks to a new position. If the queue was previously paused then it
remains paused but the item's new position is still remembered.
The request is issued in the current session.
Please refer to {@link MediaControlIntent#ACTION_SEEK ACTION_SEEK} for
more information about the semantics of this request.
if (itemId == null) {
throw new IllegalArgumentException("itemId must not be null");
}
throwIfNoCurrentSession();
Intent intent = new Intent(MediaControlIntent.ACTION_SEEK);
intent.putExtra(MediaControlIntent.EXTRA_ITEM_CONTENT_POSITION, positionMillis);
performItemAction(intent, mSessionId, itemId, extras, callback);
| public void | setSessionId(java.lang.String sessionId)Sets the current session id.
It is usually not necessary to set the session id explicitly since
it is created as a side-effect of other requests such as
{@link #play}, {@link #enqueue}, and {@link #startSession}.
if (mSessionId != sessionId
&& (mSessionId == null || !mSessionId.equals(sessionId))) {
if (DEBUG) {
Log.d(TAG, "Session id is now: " + sessionId);
}
mSessionId = sessionId;
if (mStatusCallback != null) {
mStatusCallback.onSessionChanged(sessionId);
}
}
| public void | setStatusCallback(android.support.v7.media.RemotePlaybackClient$StatusCallback callback)Sets a callback that should receive status updates when the state of
media sessions or media items created by this instance of the remote
playback client changes.
The callback should be set before the session is created or any play
commands are issued.
mStatusCallback = callback;
| public void | startSession(android.os.Bundle extras, android.support.v7.media.RemotePlaybackClient$SessionActionCallback callback)Sends a request to start a new media playback session.
The application must wait for the callback to indicate that this request
is complete before issuing other requests that affect the session. If this
request is successful then the previous session will be invalidated.
Please refer to {@link MediaControlIntent#ACTION_START_SESSION ACTION_START_SESSION}
for more information about the semantics of this request.
throwIfSessionManagementNotSupported();
Intent intent = new Intent(MediaControlIntent.ACTION_START_SESSION);
intent.putExtra(MediaControlIntent.EXTRA_SESSION_STATUS_UPDATE_RECEIVER,
mSessionStatusPendingIntent);
performSessionAction(intent, null, extras, callback);
| public void | stop(android.os.Bundle extras, android.support.v7.media.RemotePlaybackClient$SessionActionCallback callback)Sends a request to stop media playback and clear the media playback queue.
The request is issued in the current session. If the queue is already
empty then the request has no effect.
Please refer to {@link MediaControlIntent#ACTION_STOP ACTION_STOP} for
more information about the semantics of this request.
throwIfNoCurrentSession();
Intent intent = new Intent(MediaControlIntent.ACTION_STOP);
performSessionAction(intent, mSessionId, extras, callback);
| private void | throwIfNoCurrentSession()
if (mSessionId == null) {
throw new IllegalStateException("There is no current session.");
}
| private void | throwIfQueuingNotSupported()
if (!mRouteSupportsQueuing) {
throw new UnsupportedOperationException("The route does not support queuing.");
}
| private void | throwIfRemotePlaybackNotSupported()
if (!mRouteSupportsRemotePlayback) {
throw new UnsupportedOperationException("The route does not support remote playback.");
}
| private void | throwIfSessionManagementNotSupported()
if (!mRouteSupportsSessionManagement) {
throw new UnsupportedOperationException("The route does not support "
+ "session management.");
}
|
|