FileDocCategorySizeDatePackage
MediaControllerCompat.javaAPI DocAndroid 5.1 API44821Thu Mar 12 22:22:56 GMT 2015android.support.v4.media.session

MediaControllerCompat

public final class MediaControllerCompat extends Object
Allows an app to interact with an ongoing media session. Media buttons and other commands can be sent to the session. A callback may be registered to receive updates from the session, such as metadata and play state changes.

A MediaController can be created if you have a {@link MediaSessionCompat.Token} from the session owner.

MediaController objects are thread-safe.

This is a helper for accessing features in {@link android.media.session.MediaSession} introduced after API level 4 in a backwards compatible fashion.

Fields Summary
private static final String
TAG
private final MediaControllerImpl
mImpl
private final MediaSessionCompat.Token
mToken
Constructors Summary
public MediaControllerCompat(android.content.Context context, MediaSessionCompat session)
Creates a media controller from a session.

param
session The session to be controlled.


                       
         
        if (session == null) {
            throw new IllegalArgumentException("session must not be null");
        }
        mToken = session.getSessionToken();

        if (android.os.Build.VERSION.SDK_INT >= 21) {
            mImpl = new MediaControllerImplApi21(context, session);
        } else {
            mImpl = new MediaControllerImplBase(mToken);
        }
    
public MediaControllerCompat(android.content.Context context, MediaSessionCompat.Token sessionToken)
Creates a media controller from a session token which may have been obtained from another process.

param
sessionToken The token of the session to be controlled.
throws
RemoteException if the session is not accessible.

        if (sessionToken == null) {
            throw new IllegalArgumentException("sessionToken must not be null");
        }
        mToken = sessionToken;

        if (android.os.Build.VERSION.SDK_INT >= 21) {
            mImpl = new MediaControllerImplApi21(context, sessionToken);
        } else {
            mImpl = new MediaControllerImplBase(mToken);
        }
    
Methods Summary
public voidadjustVolume(int direction, int flags)
Adjust the volume of the output this session is playing on. The direction must be one of {@link AudioManager#ADJUST_LOWER}, {@link AudioManager#ADJUST_RAISE}, or {@link AudioManager#ADJUST_SAME}. The command will be ignored if the session does not support {@link VolumeProviderCompat#VOLUME_CONTROL_RELATIVE} or {@link VolumeProviderCompat#VOLUME_CONTROL_ABSOLUTE}. The flags in {@link AudioManager} may be used to affect the handling.

see
#getPlaybackInfo()
param
direction The direction to adjust the volume in.
param
flags Any flags to pass with the command.

        mImpl.adjustVolume(direction, flags);
    
public booleandispatchMediaButtonEvent(android.view.KeyEvent keyEvent)
Send the specified media button event to the session. Only media keys can be sent by this method, other keys will be ignored.

param
keyEvent The media button event to dispatch.
return
true if the event was sent to the session, false otherwise.

        if (keyEvent == null) {
            throw new IllegalArgumentException("KeyEvent may not be null");
        }
        return mImpl.dispatchMediaButtonEvent(keyEvent);
    
public android.os.BundlegetExtras()
Get the extras for this session.

        return mImpl.getExtras();
    
public longgetFlags()
Get the flags for this session. Flags are defined in {@link MediaSessionCompat}.

return
The current set of flags for the session.

        return mImpl.getFlags();
    
public java.lang.ObjectgetMediaController()
Gets the underlying framework {@link android.media.session.MediaController} object.

This method is only supported on API 21+.

return
The underlying {@link android.media.session.MediaController} object, or null if none.

        return mImpl.getMediaController();
    
public android.support.v4.media.MediaMetadataCompatgetMetadata()
Get the current metadata for this session.

return
The current MediaMetadata or null.

        return mImpl.getMetadata();
    
public java.lang.StringgetPackageName()
Get the session owner's package name.

return
The package name of of the session owner.

        return mImpl.getPackageName();
    
public android.support.v4.media.session.MediaControllerCompat$PlaybackInfogetPlaybackInfo()
Get the current playback info for this session.

return
The current playback info or null.

        return mImpl.getPlaybackInfo();
    
public PlaybackStateCompatgetPlaybackState()
Get the current playback state for this session.

return
The current PlaybackState or null

        return mImpl.getPlaybackState();
    
public java.util.ListgetQueue()
Get the current play queue for this session if one is set. If you only care about the current item {@link #getMetadata()} should be used.

return
The current play queue or null.

        return mImpl.getQueue();
    
public java.lang.CharSequencegetQueueTitle()
Get the queue title for this session.

        return mImpl.getQueueTitle();
    
public intgetRatingType()
Get the rating type supported by the session. One of:
  • {@link RatingCompat#RATING_NONE}
  • {@link RatingCompat#RATING_HEART}
  • {@link RatingCompat#RATING_THUMB_UP_DOWN}
  • {@link RatingCompat#RATING_3_STARS}
  • {@link RatingCompat#RATING_4_STARS}
  • {@link RatingCompat#RATING_5_STARS}
  • {@link RatingCompat#RATING_PERCENTAGE}

return
The supported rating type

        return mImpl.getRatingType();
    
public android.app.PendingIntentgetSessionActivity()
Get an intent for launching UI associated with this session if one exists.

return
A {@link PendingIntent} to launch UI or null.

        return mImpl.getSessionActivity();
    
public MediaSessionCompat.TokengetSessionToken()
Get the token for the session this controller is connected to.

return
The session's token.

        return mToken;
    
public android.support.v4.media.session.MediaControllerCompat$TransportControlsgetTransportControls()
Get a {@link TransportControls} instance for this session.

return
A controls instance

        return mImpl.getTransportControls();
    
public voidregisterCallback(android.support.v4.media.session.MediaControllerCompat$Callback callback)
Adds a callback to receive updates from the Session. Updates will be posted on the caller's thread.

param
callback The callback object, must not be null.

        registerCallback(callback, null);
    
public voidregisterCallback(android.support.v4.media.session.MediaControllerCompat$Callback callback, android.os.Handler handler)
Adds a callback to receive updates from the session. Updates will be posted on the specified handler's thread.

param
callback The callback object, must not be null.
param
handler The handler to post updates on. If null the callers thread will be used.

        if (callback == null) {
            throw new IllegalArgumentException("callback cannot be null");
        }
        if (handler == null) {
            handler = new Handler();
        }
        mImpl.registerCallback(callback, handler);
    
public voidsendCommand(java.lang.String command, android.os.Bundle params, android.os.ResultReceiver cb)
Sends a generic command to the session. It is up to the session creator to decide what commands and parameters they will support. As such, commands should only be sent to sessions that the controller owns.

param
command The command to send
param
params Any parameters to include with the command
param
cb The callback to receive the result on

        if (TextUtils.isEmpty(command)) {
            throw new IllegalArgumentException("command cannot be null or empty");
        }
        mImpl.sendCommand(command, params, cb);
    
public voidsetVolumeTo(int value, int flags)
Set the volume of the output this session is playing on. The command will be ignored if it does not support {@link VolumeProviderCompat#VOLUME_CONTROL_ABSOLUTE}. The flags in {@link AudioManager} may be used to affect the handling.

see
#getPlaybackInfo()
param
value The value to set it to, between 0 and the reported max.
param
flags Flags from {@link AudioManager} to include with the volume request.

        mImpl.setVolumeTo(value, flags);
    
public voidunregisterCallback(android.support.v4.media.session.MediaControllerCompat$Callback callback)
Stop receiving updates on the specified callback. If an update has already been posted you may still receive it after calling this method.

param
callback The callback to remove

        if (callback == null) {
            throw new IllegalArgumentException("callback cannot be null");
        }
        mImpl.unregisterCallback(callback);