Added in API level 16

MediaRouter


open class MediaRouter
kotlin.Any
   ↳ 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
abstract

Interface for receiving events about media routing changes.

open

Definition of a category of routes.

open

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

open

Information about a media route.

open

Stub implementation of MediaRouter.Callback.

open

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

abstract

Interface for receiving events about volume changes.

Constants
static Int

Flag for #addCallback: Actively scan for routes while this callback is registered.

static Int

Flag for #addCallback: Do not filter route events.

static Int

Route type flag for live audio.

static Int

Route type flag for live video.

static Int

Route type flag for application-specific usage.

Public methods
open Unit

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

open Unit
addCallback(types: Int, cb: MediaRouter.Callback!, flags: Int)

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

open Unit

Add an app-specified route for media to the MediaRouter.

open Unit

Remove all app-specified routes from the MediaRouter.

open MediaRouter.RouteCategory!
createRouteCategory(name: CharSequence!, isGroupable: Boolean)

Create a new route category.

open MediaRouter.RouteCategory!
createRouteCategory(nameResId: Int, isGroupable: Boolean)

Create a new route category.

open MediaRouter.UserRouteInfo!

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

open MediaRouter.RouteCategory!

Return the category at the given index.

open Int

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

open MediaRouter.RouteInfo!

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

open MediaRouter.RouteInfo!
getRouteAt(index: Int)

Return the route at the specified index.

open Int

Return the number of routes currently known to this MediaRouter.

open MediaRouter.RouteInfo!

Return the currently selected route for any of the given types

open Unit

Remove the specified callback.

open Unit

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

open Unit

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

Constants

CALLBACK_FLAG_PERFORM_ACTIVE_SCAN

Added in API level 18
static val CALLBACK_FLAG_PERFORM_ACTIVE_SCAN: Int

Flag for #addCallback: 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.

Value: 1

CALLBACK_FLAG_UNFILTERED_EVENTS

Added in API level 18
static val CALLBACK_FLAG_UNFILTERED_EVENTS: Int

Flag for #addCallback: 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.

Value: 2

ROUTE_TYPE_LIVE_AUDIO

Added in API level 16
static val ROUTE_TYPE_LIVE_AUDIO: Int

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.

Value: 1

ROUTE_TYPE_LIVE_VIDEO

Added in API level 17
static val ROUTE_TYPE_LIVE_VIDEO: Int

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.

Value: 2

ROUTE_TYPE_USER

Added in API level 16
static val ROUTE_TYPE_USER: Int

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.

Value: 8388608

Public methods

addCallback

Added in API level 16
open fun addCallback(
    types: Int,
    cb: MediaRouter.Callback!
): Unit

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

addCallback

Added in API level 18
open fun addCallback(
    types: Int,
    cb: MediaRouter.Callback!,
    flags: Int
): Unit

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.

addUserRoute

Added in API level 16
open fun addUserRoute(info: MediaRouter.UserRouteInfo!): Unit

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
open fun clearUserRoutes(): Unit

Remove all app-specified routes from the MediaRouter.

createRouteCategory

Added in API level 16
open fun createRouteCategory(
    name: CharSequence!,
    isGroupable: Boolean
): MediaRouter.RouteCategory!

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
Return
MediaRouter.RouteCategory! the new RouteCategory

createRouteCategory

Added in API level 16
open fun createRouteCategory(
    nameResId: Int,
    isGroupable: Boolean
): MediaRouter.RouteCategory!

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
Return
MediaRouter.RouteCategory! the new RouteCategory

createUserRoute

Added in API level 16
open fun createUserRoute(category: MediaRouter.RouteCategory!): MediaRouter.UserRouteInfo!

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
Return
MediaRouter.UserRouteInfo! A new UserRouteInfo for use by the application

getCategoryAt

Added in API level 16
open fun getCategoryAt(index: Int): MediaRouter.RouteCategory!

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

Parameters
index Int: which category to return
Return
MediaRouter.RouteCategory! the category at index

getCategoryCount

Added in API level 16
open fun getCategoryCount(): Int

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

Return
Int the number of unique categories represented by this MediaRouter's known routes

getDefaultRoute

Added in API level 18
open fun getDefaultRoute(): MediaRouter.RouteInfo!

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

The system always provides a default route.

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

getRouteAt

Added in API level 16
open fun getRouteAt(index: Int): MediaRouter.RouteInfo!

Return the route at the specified index.

Parameters
index Int: index of the route to return
Return
MediaRouter.RouteInfo! the route at index

getRouteCount

Added in API level 16
open fun getRouteCount(): Int

Return the number of routes currently known to this MediaRouter.

Return
Int the number of routes tracked by this router

getSelectedRoute

Added in API level 16
open fun getSelectedRoute(type: Int): MediaRouter.RouteInfo!

Return the currently selected route for any of the given types

Parameters
type Int: route types
Return
MediaRouter.RouteInfo! the selected route

removeCallback

Added in API level 16
open fun removeCallback(cb: MediaRouter.Callback!): Unit

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
open fun removeUserRoute(info: MediaRouter.UserRouteInfo!): Unit

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
open fun selectRoute(
    types: Int,
    route: MediaRouter.RouteInfo
): Unit

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.
Exceptions
java.lang.IllegalArgumentException if the given route is null