MediaSessionManagerpublic final class MediaSessionManager extends Object Provides support for interacting with {@link MediaSession media sessions}
that applications have published to express their ongoing media playback
state.
Use Context.getSystemService(Context.MEDIA_SESSION_SERVICE) to
get an instance of this class. |
Fields Summary |
---|
private static final String | TAG | private final android.util.ArrayMap | mListeners | private final Object | mLock | private final android.media.session.ISessionManager | mService | private android.content.Context | mContext | public static final int | DIRECTION_MUTESpecial flag for sending the mute key to dispatchAdjustVolume used by the
system. |
Constructors Summary |
---|
public MediaSessionManager(android.content.Context context)
// Consider rewriting like DisplayManagerGlobal
// Decide if we need context
mContext = context;
IBinder b = ServiceManager.getService(Context.MEDIA_SESSION_SERVICE);
mService = ISessionManager.Stub.asInterface(b);
|
Methods Summary |
---|
public void | addOnActiveSessionsChangedListener(android.media.session.MediaSessionManager$OnActiveSessionsChangedListener sessionListener, android.content.ComponentName notificationListener)Add a listener to be notified when the list of active sessions
changes.This requires the
android.Manifest.permission.MEDIA_CONTENT_CONTROL permission be held by
the calling app. You may also retrieve this list if your app is an
enabled notification listener using the
{@link NotificationListenerService} APIs, in which case you must pass the
{@link ComponentName} of your enabled listener. Updates will be posted to
the thread that registered the listener.
addOnActiveSessionsChangedListener(sessionListener, notificationListener, null);
| public void | addOnActiveSessionsChangedListener(android.media.session.MediaSessionManager$OnActiveSessionsChangedListener sessionListener, android.content.ComponentName notificationListener, android.os.Handler handler)Add a listener to be notified when the list of active sessions
changes.This requires the
android.Manifest.permission.MEDIA_CONTENT_CONTROL permission be held by
the calling app. You may also retrieve this list if your app is an
enabled notification listener using the
{@link NotificationListenerService} APIs, in which case you must pass the
{@link ComponentName} of your enabled listener. Updates will be posted to
the handler specified or to the caller's thread if the handler is null.
addOnActiveSessionsChangedListener(sessionListener, notificationListener,
UserHandle.myUserId(), handler);
| public void | addOnActiveSessionsChangedListener(android.media.session.MediaSessionManager$OnActiveSessionsChangedListener sessionListener, android.content.ComponentName notificationListener, int userId, android.os.Handler handler)Add a listener to be notified when the list of active sessions
changes.This requires the
android.Manifest.permission.MEDIA_CONTENT_CONTROL permission be held by
the calling app. You may also retrieve this list if your app is an
enabled notification listener using the
{@link NotificationListenerService} APIs, in which case you must pass the
{@link ComponentName} of your enabled listener.
if (sessionListener == null) {
throw new IllegalArgumentException("listener may not be null");
}
if (handler == null) {
handler = new Handler();
}
synchronized (mLock) {
if (mListeners.get(sessionListener) != null) {
Log.w(TAG, "Attempted to add session listener twice, ignoring.");
return;
}
SessionsChangedWrapper wrapper = new SessionsChangedWrapper(sessionListener, handler);
try {
mService.addSessionsListener(wrapper.mStub, notificationListener, userId);
mListeners.put(sessionListener, wrapper);
} catch (RemoteException e) {
Log.e(TAG, "Error in addOnActiveSessionsChangedListener.", e);
}
}
| public ISession | createSession(MediaSession.CallbackStub cbStub, java.lang.String tag, int userId)Create a new session in the system and get the binder for it.
return mService.createSession(mContext.getPackageName(), cbStub, tag, userId);
| public void | dispatchAdjustVolume(int suggestedStream, int direction, int flags)Dispatch an adjust volume request to the system. It will be sent to the
most relevant audio stream or media session. The direction must be one of
{@link AudioManager#ADJUST_LOWER}, {@link AudioManager#ADJUST_RAISE},
{@link AudioManager#ADJUST_SAME}.
try {
mService.dispatchAdjustVolume(suggestedStream, direction, flags);
} catch (RemoteException e) {
Log.e(TAG, "Failed to send adjust volume.", e);
}
| public void | dispatchMediaKeyEvent(android.view.KeyEvent keyEvent)Send a media key event. The receiver will be selected automatically.
dispatchMediaKeyEvent(keyEvent, false);
| public void | dispatchMediaKeyEvent(android.view.KeyEvent keyEvent, boolean needWakeLock)Send a media key event. The receiver will be selected automatically.
try {
mService.dispatchMediaKeyEvent(keyEvent, needWakeLock);
} catch (RemoteException e) {
Log.e(TAG, "Failed to send key event.", e);
}
| public java.util.List | getActiveSessions(android.content.ComponentName notificationListener)Get a list of controllers for all ongoing sessions. The controllers will
be provided in priority order with the most important controller at index
0.
This requires the android.Manifest.permission.MEDIA_CONTENT_CONTROL
permission be held by the calling app. You may also retrieve this list if
your app is an enabled notification listener using the
{@link NotificationListenerService} APIs, in which case you must pass the
{@link ComponentName} of your enabled listener.
return getActiveSessionsForUser(notificationListener, UserHandle.myUserId());
| public java.util.List | getActiveSessionsForUser(android.content.ComponentName notificationListener, int userId)Get active sessions for a specific user. To retrieve actions for a user
other than your own you must hold the
{@link android.Manifest.permission#INTERACT_ACROSS_USERS_FULL} permission
in addition to any other requirements. If you are an enabled notification
listener you may only get sessions for the users you are enabled for.
ArrayList<MediaController> controllers = new ArrayList<MediaController>();
try {
List<IBinder> binders = mService.getSessions(notificationListener, userId);
int size = binders.size();
for (int i = 0; i < size; i++) {
MediaController controller = new MediaController(mContext, ISessionController.Stub
.asInterface(binders.get(i)));
controllers.add(controller);
}
} catch (RemoteException e) {
Log.e(TAG, "Failed to get active sessions: ", e);
}
return controllers;
| public boolean | isGlobalPriorityActive()Check if the global priority session is currently active. This can be
used to decide if media keys should be sent to the session or to the app.
try {
return mService.isGlobalPriorityActive();
} catch (RemoteException e) {
Log.e(TAG, "Failed to check if the global priority is active.", e);
}
return false;
| public void | removeOnActiveSessionsChangedListener(android.media.session.MediaSessionManager$OnActiveSessionsChangedListener listener)Stop receiving active sessions updates on the specified listener.
if (listener == null) {
throw new IllegalArgumentException("listener may not be null");
}
synchronized (mLock) {
SessionsChangedWrapper wrapper = mListeners.remove(listener);
if (wrapper != null) {
try {
mService.removeSessionsListener(wrapper.mStub);
} catch (RemoteException e) {
Log.e(TAG, "Error in removeOnActiveSessionsChangedListener.", e);
}
}
}
| public void | setRemoteVolumeController(android.media.IRemoteVolumeController rvc)Set the remote volume controller to receive volume updates on. Only for
use by system UI.
try {
mService.setRemoteVolumeController(rvc);
} catch (RemoteException e) {
Log.e(TAG, "Error in setRemoteVolumeController.", e);
}
|
|