MediaRouter


public class MediaRouter
extends Object

java.lang.Object
   ↳ android.media.MediaRouter


This API is not recommended for new applications. Use the AndroidX Media Router Library for consistent behavior across all devices.

MediaRouter allows applications to control the routing of media channels and streams from the current device to external speakers and destination devices.

A MediaRouter is retrieved through Context.getSystemService() of a Context.MEDIA_ROUTER_SERVICE.

This API is not thread-safe; all interactions with it must be done from the main thread of the process.

Summary

Nested classes

class MediaRouter.Callback

Interface for receiving events about media routing changes. 

class MediaRouter.RouteCategory

Definition of a category of routes. 

class MediaRouter.RouteGroup

Information about a route that consists of multiple other routes in a group. 

class MediaRouter.RouteInfo

Information about a media route. 

class MediaRouter.SimpleCallback

Stub implementation of MediaRouter.Callback

class MediaRouter.UserRouteInfo

Information about a route that the application may define and modify. 

class MediaRouter.VolumeCallback

Interface for receiving events about volume changes. 

Constants

int CALLBACK_FLAG_PERFORM_ACTIVE_SCAN

Flag for addCallback(int, Callback): Actively scan for routes while this callback is registered.

int CALLBACK_FLAG_UNFILTERED_EVENTS

Flag for addCallback(int, Callback): Do not filter route events.

int ROUTE_TYPE_LIVE_AUDIO

Route type flag for live audio.

int ROUTE_TYPE_LIVE_VIDEO

Route type flag for live video.

int ROUTE_TYPE_USER

Route type flag for application-specific usage.

Public methods

void addCallback(int types, MediaRouter.Callback cb, int flags)

Add a callback to listen to events about specific kinds of media routes.

void addCallback(int types, MediaRouter.Callback cb)

Add a callback to listen to events about specific kinds of media routes.

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(int nameResId, boolean isGroupable)

Create a new route category.

MediaRouter.RouteCategory createRouteCategory(CharSequence name, 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(int type)

Return the currently selected route for any of the given types

void removeCallback(MediaRouter.Callback cb)

Remove the specified callback.

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.

Inherited methods

Constants

CALLBACK_FLAG_PERFORM_ACTIVE_SCAN

Added in API level 18
public static final int CALLBACK_FLAG_PERFORM_ACTIVE_SCAN

Flag for addCallback(int, 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.

Constant Value: 1 (0x00000001)

CALLBACK_FLAG_UNFILTERED_EVENTS

Added in API level 18
public static final int CALLBACK_FLAG_UNFILTERED_EVENTS

Flag for addCallback(int, 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.

Constant Value: 2 (0x00000002)

ROUTE_TYPE_LIVE_AUDIO

Added in API level 16
public static final int ROUTE_TYPE_LIVE_AUDIO

Route type flag for 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.

Constant Value: 1 (0x00000001)

ROUTE_TYPE_LIVE_VIDEO

Added in API level 17
public static final int ROUTE_TYPE_LIVE_VIDEO

Route type flag for 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.

Constant Value: 2 (0x00000002)

ROUTE_TYPE_USER

Added in API level 16
public static final int ROUTE_TYPE_USER

Route type flag for application-specific usage.

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.

Constant Value: 8388608 (0x00800000)

Public methods

addCallback

Added in API level 18
public void addCallback (int types, 
                MediaRouter.Callback cb, 
                int flags)

Add a callback to listen to events about specific kinds of media routes. If the specified callback is already registered, its registration will be updated for any additional route types specified.

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.

Parameters
types int: Types of routes this callback is interested in

cb MediaRouter.Callback: Callback to add

flags int: 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.

addCallback

Added in API level 16
public void addCallback (int types, 
                MediaRouter.Callback cb)

Add a callback to listen to events about specific kinds of media routes. If the specified callback is already registered, its registration will be updated for any additional route types specified.

This is a convenience method that has the same effect as calling addCallback(int, android.media.MediaRouter.Callback, int) without flags.

Parameters
types int: Types of routes this callback is interested in

cb MediaRouter.Callback: Callback to add

addUserRoute

Added in API level 16
public void addUserRoute (MediaRouter.UserRouteInfo info)

Add an app-specified route for media to the MediaRouter. App-specified route definitions are created using createUserRoute(android.media.MediaRouter.RouteCategory)

Parameters
info MediaRouter.UserRouteInfo: Definition of the route to add

clearUserRoutes

Added in API level 16
public void clearUserRoutes ()

Remove all app-specified routes from the MediaRouter.

createRouteCategory

Added in API level 16
public MediaRouter.RouteCategory createRouteCategory (int nameResId, 
                boolean isGroupable)

Create a new route category. Each route must belong to a category.

Parameters
nameResId int: Resource ID of the name of the new category

isGroupable boolean: true if routes in this category may be grouped with one another

Returns
MediaRouter.RouteCategory the new RouteCategory

createRouteCategory

Added in API level 16
public MediaRouter.RouteCategory createRouteCategory (CharSequence name, 
                boolean isGroupable)

Create a new route category. Each route must belong to a category.

Parameters
name CharSequence: Name of the new category

isGroupable boolean: true if routes in this category may be grouped with one another

Returns
MediaRouter.RouteCategory the new RouteCategory

createUserRoute

Added in API level 16
public MediaRouter.UserRouteInfo createUserRoute (MediaRouter.RouteCategory category)

Create a new user route that may be modified and registered for use by the application.

Parameters
category MediaRouter.RouteCategory: The category the new route will belong to

Returns
MediaRouter.UserRouteInfo A new UserRouteInfo for use by the application

getCategoryAt

Added in API level 16
public MediaRouter.RouteCategory getCategoryAt (int index)

Return the category at the given index. Valid indices are in the range [0-getCategoryCount).

Parameters
index int: which category to return

Returns
MediaRouter.RouteCategory the category at index

getCategoryCount

Added in API level 16
public int getCategoryCount ()

Return the number of categories currently represented by routes known to this MediaRouter.

Returns
int the number of unique categories represented by this MediaRouter's known routes

getDefaultRoute

Added in API level 18
public MediaRouter.RouteInfo getDefaultRoute ()

Gets the default route for playing media content on the system.

The system always provides a default route.

Returns
MediaRouter.RouteInfo The default route, which is guaranteed to never be null.

getRouteAt

Added in API level 16
public MediaRouter.RouteInfo getRouteAt (int index)

Return the route at the specified index.

Parameters
index int: index of the route to return

Returns
MediaRouter.RouteInfo the route at index

getRouteCount

Added in API level 16
public int getRouteCount ()

Return the number of routes currently known to this MediaRouter.

Returns
int the number of routes tracked by this router

getSelectedRoute

Added in API level 16
public MediaRouter.RouteInfo getSelectedRoute (int type)

Return the currently selected route for any of the given types

Parameters
type int: route types

Returns
MediaRouter.RouteInfo the selected route

removeCallback

Added in API level 16
public void removeCallback (MediaRouter.Callback cb)

Remove the specified callback. It will no longer receive events about media routing.

Parameters
cb MediaRouter.Callback: Callback to remove

removeUserRoute

Added in API level 16
public void removeUserRoute (MediaRouter.UserRouteInfo info)

Remove an app-specified route for media from the MediaRouter.

Parameters
info MediaRouter.UserRouteInfo: Definition of the route to remove

selectRoute

Added in API level 16
public void selectRoute (int types, 
                MediaRouter.RouteInfo route)

Select the specified route to use for output of the given media types.

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.

Parameters
types int: type flags indicating which types this route should be used for. The route must support at least a subset.

route MediaRouter.RouteInfo: Route to select This value cannot be null.

Throws
IllegalArgumentException if the given route is null