public class MediaRouter extends Object
A MediaRouter is retrieved through Context.getSystemService()
of a Context.MEDIA_ROUTER_SERVICE
.
The media router API is not thread-safe; all interactions with it must be done from the main thread of the process.
Modifier and Type | Class and Description |
---|---|
static class |
MediaRouter.Callback
Interface for receiving events about media routing changes.
|
static class |
MediaRouter.RouteCategory
Definition of a category of routes.
|
static class |
MediaRouter.RouteGroup
Information about a route that consists of multiple other routes in a group.
|
static class |
MediaRouter.RouteInfo
Information about a media route.
|
static class |
MediaRouter.SimpleCallback
Stub implementation of
MediaRouter.Callback . |
static class |
MediaRouter.UserRouteInfo
Information about a route that the application may define and modify.
|
static class |
MediaRouter.VolumeCallback
Interface for receiving events about volume changes.
|
Modifier and Type | Field and Description |
---|---|
static int |
AVAILABILITY_FLAG_IGNORE_DEFAULT_ROUTE
Flag for
isRouteAvailable(int, int) : Ignore the default route. |
static int |
CALLBACK_FLAG_PASSIVE_DISCOVERY
Requests that discovery be performed but only if there is some other active
callback already registered.
|
static int |
CALLBACK_FLAG_PERFORM_ACTIVE_SCAN
Flag for
addCallback(int, android.media.MediaRouter.Callback) : Actively scan for routes while this callback
is registered. |
static int |
CALLBACK_FLAG_REQUEST_DISCOVERY
Explicitly requests discovery.
|
static int |
CALLBACK_FLAG_UNFILTERED_EVENTS
Flag for
addCallback(int, android.media.MediaRouter.Callback) : Do not filter route events. |
static int |
ROUTE_TYPE_LIVE_AUDIO
Route type flag for live audio.
|
static int |
ROUTE_TYPE_LIVE_VIDEO
Route type flag for live video.
|
static int |
ROUTE_TYPE_REMOTE_DISPLAY
Temporary interop constant to identify remote displays.
|
static int |
ROUTE_TYPE_USER
Route type flag for application-specific usage.
|
Constructor and Description |
---|
MediaRouter(Context context) |
Modifier and Type | Method and Description |
---|---|
void |
addCallback(int types,
MediaRouter.Callback cb)
Add a callback to listen to events about specific kinds of media routes.
|
void |
addCallback(int types,
MediaRouter.Callback cb,
int flags)
Add a callback to listen to events about specific kinds of media routes.
|
void |
addRouteInt(MediaRouter.RouteInfo info) |
void |
addUserRoute(MediaRouter.UserRouteInfo info)
Add an app-specified route for media to the MediaRouter.
|
void |
clearUserRoutes()
Remove all app-specified routes from the MediaRouter.
|
MediaRouter.RouteCategory |
createRouteCategory(CharSequence name,
boolean isGroupable)
Create a new route category.
|
MediaRouter.RouteCategory |
createRouteCategory(int nameResId,
boolean isGroupable)
Create a new route category.
|
MediaRouter.UserRouteInfo |
createUserRoute(MediaRouter.RouteCategory category)
Create a new user route that may be modified and registered for use by the application.
|
MediaRouter.RouteCategory |
getCategoryAt(int index)
Return the
category at the given index. |
int |
getCategoryCount()
Return the number of
categories currently
represented by routes known to this MediaRouter. |
MediaRouter.RouteInfo |
getDefaultRoute()
Gets the default route for playing media content on the system.
|
MediaRouter.RouteInfo |
getRouteAt(int index)
Return the route at the specified index.
|
int |
getRouteCount()
Return the number of
routes currently known
to this MediaRouter. |
MediaRouter.RouteInfo |
getSelectedRoute() |
MediaRouter.RouteInfo |
getSelectedRoute(int type)
Return the currently selected route for any of the given types
|
MediaRouter.RouteCategory |
getSystemCategory() |
boolean |
isRouteAvailable(int types,
int flags)
Returns true if there is a route that matches the specified types.
|
void |
rebindAsUser(int userId)
Rebinds the media router to handle routes that belong to the specified user.
|
void |
removeCallback(MediaRouter.Callback cb)
Remove the specified callback.
|
void |
removeRouteInt(MediaRouter.RouteInfo info) |
void |
removeUserRoute(MediaRouter.UserRouteInfo info)
Remove an app-specified route for media from the MediaRouter.
|
void |
selectRoute(int types,
MediaRouter.RouteInfo route)
Select the specified route to use for output of the given media types.
|
void |
selectRouteInt(int types,
MediaRouter.RouteInfo route,
boolean explicit) |
public static final int ROUTE_TYPE_LIVE_AUDIO
A device that supports live audio routing will allow the media audio stream to be routed to supported destinations. This can include internal speakers or audio jacks on the device itself, A2DP devices, and more.
Once initiated this routing is transparent to the application. All audio played on the media stream will be routed to the selected destination.
public static final int ROUTE_TYPE_LIVE_VIDEO
A device that supports live video routing will allow a mirrored version
of the device's primary display or a customized
Presentation
to be routed to supported destinations.
Once initiated, display mirroring is transparent to the application.
While remote routing is active the application may use a
Presentation
to replace the mirrored view
on the external display with different content.
public static final int ROUTE_TYPE_REMOTE_DISPLAY
public static final int ROUTE_TYPE_USER
Unlike other media route types, user routes are managed by the application. The MediaRouter will manage and dispatch events for user routes, but the application is expected to interpret the meaning of these events and perform the requested routing tasks.
public static final int CALLBACK_FLAG_PERFORM_ACTIVE_SCAN
addCallback(int, android.media.MediaRouter.Callback)
: Actively scan for routes while this callback
is registered.
When this flag is specified, the media router will actively scan for new routes. Certain routes, such as wifi display routes, may not be discoverable except when actively scanning. This flag is typically used when the route picker dialog has been opened by the user to ensure that the route information is up to date.
Active scanning may consume a significant amount of power and may have intrusive effects on wireless connectivity. Therefore it is important that active scanning only be requested when it is actually needed to satisfy a user request to discover and select a new route.
public static final int CALLBACK_FLAG_UNFILTERED_EVENTS
addCallback(int, android.media.MediaRouter.Callback)
: Do not filter route events.
When this flag is specified, the callback will be invoked for event that affect any route even if they do not match the callback's filter.
public static final int CALLBACK_FLAG_REQUEST_DISCOVERY
public static final int CALLBACK_FLAG_PASSIVE_DISCOVERY
public static final int AVAILABILITY_FLAG_IGNORE_DEFAULT_ROUTE
isRouteAvailable(int, int)
: Ignore the default route.
This flag is used to determine whether a matching non-default route is available. This constraint may be used to decide whether to offer the route chooser dialog to the user. There is no point offering the chooser if there are no non-default choices.
public MediaRouter(Context context)
public MediaRouter.RouteInfo getDefaultRoute()
The system always provides a default route.
public MediaRouter.RouteCategory getSystemCategory()
public MediaRouter.RouteInfo getSelectedRoute()
public MediaRouter.RouteInfo getSelectedRoute(int type)
type
- route typespublic boolean isRouteAvailable(int types, int flags)
This method returns true if there are any available routes that match the types
regardless of whether they are enabled or disabled. If the
AVAILABILITY_FLAG_IGNORE_DEFAULT_ROUTE
flag is specified, then
the method will only consider non-default routes.
types
- The types to match.flags
- Flags to control the determination of whether a route may be available.
May be zero or AVAILABILITY_FLAG_IGNORE_DEFAULT_ROUTE
.public void addCallback(int types, MediaRouter.Callback cb)
This is a convenience method that has the same effect as calling
addCallback(int, Callback, int)
without flags.
types
- Types of routes this callback is interested incb
- Callback to addpublic void addCallback(int types, MediaRouter.Callback cb, int flags)
By default, the callback will only be invoked for events that affect routes
that match the specified selector. The filtering may be disabled by specifying
the CALLBACK_FLAG_UNFILTERED_EVENTS
flag.
types
- Types of routes this callback is interested incb
- Callback to addflags
- Flags to control the behavior of the callback.
May be zero or a combination of CALLBACK_FLAG_PERFORM_ACTIVE_SCAN
and
CALLBACK_FLAG_UNFILTERED_EVENTS
.public void removeCallback(MediaRouter.Callback cb)
cb
- Callback to removepublic void selectRoute(int types, MediaRouter.RouteInfo route)
As API version 18, this function may be used to select any route. In prior versions, this function could only be used to select user routes and would ignore any attempt to select a system route.
types
- type flags indicating which types this route should be used for.
The route must support at least a subset.route
- Route to selectIllegalArgumentException
- if the given route is null
public void selectRouteInt(int types, MediaRouter.RouteInfo route, boolean explicit)
public void addUserRoute(MediaRouter.UserRouteInfo info)
createUserRoute(RouteCategory)
info
- Definition of the route to addcreateUserRoute(RouteCategory)
,
removeUserRoute(UserRouteInfo)
public void addRouteInt(MediaRouter.RouteInfo info)
public void removeUserRoute(MediaRouter.UserRouteInfo info)
info
- Definition of the route to removeaddUserRoute(UserRouteInfo)
public void clearUserRoutes()
removeUserRoute(UserRouteInfo)
public void removeRouteInt(MediaRouter.RouteInfo info)
public int getCategoryCount()
categories
currently
represented by routes known to this MediaRouter.public MediaRouter.RouteCategory getCategoryAt(int index)
category
at the given index.
Valid indices are in the range [0-getCategoryCount).index
- which category to returnpublic int getRouteCount()
routes
currently known
to this MediaRouter.public MediaRouter.RouteInfo getRouteAt(int index)
index
- index of the route to returnpublic MediaRouter.UserRouteInfo createUserRoute(MediaRouter.RouteCategory category)
category
- The category the new route will belong toaddUserRoute(UserRouteInfo)
,
removeUserRoute(UserRouteInfo)
,
createRouteCategory(CharSequence, boolean)
public MediaRouter.RouteCategory createRouteCategory(CharSequence name, boolean isGroupable)
name
- Name of the new categoryisGroupable
- true if routes in this category may be grouped with one anotherpublic MediaRouter.RouteCategory createRouteCategory(int nameResId, boolean isGroupable)
nameResId
- Resource ID of the name of the new categoryisGroupable
- true if routes in this category may be grouped with one anotherpublic void rebindAsUser(int userId)
This method is a complete hack to work around the singleton nature of the media router when running inside of singleton processes like QuickSettings. This mechanism should be burned to the ground when MediaRouter is redesigned. Ideally the current user would be pulled from the Context but we need to break down MediaRouter.Static before we can get there.