Courtesy: GitHub

Android BasicMediaRouter Sample

This sample demonstrates the use of the MediaRouter API to display content on a secondary display. Use the "Media Route Action Item" in the ActionBar to select an output device. If your device supports Miracast wireless displays, you may need to enable "Wireless Display" functionality in the system settings. Secondary screen simulation can also be enabled from the "Developer Options".

Once connected, use the "Change Color" button to change the background color of the secondary screen.

Pre-requisites

  • Android SDK 26
  • Android Build Tools v26.0.1
  • Android Support Repository

Getting Started

This sample uses the Gradle build system. To build this project, use the "gradlew build" command or use "Import Project" in Android Studio.

Support

If you've found an error in this sample, please file an issue: https://github.com/googlesamples/android-BasicMediaRouter

Patches are encouraged, and may be submitted by forking this project and submitting a pull request through GitHub. Please see CONTRIBUTING.md for more details.

License

Copyright 2017 The Android Open Source Project, Inc.

Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

compile "com.android.support:support-v4:26.1.0"

compile "com.android.support:support-v13:26.1.0"

compile "com.android.support:cardview-v7:26.1.0"

compile "com.android.support:appcompat-v7:26.1.0"

compileSdkVersion 26

minSdkVersion 17

targetSdkVersion 26

package com.example.android.basicmediarouter.tests

versionCode 1

versionName 1.0

package com.example.android.basicmediarouter

versionCode 1

versionName 1.0

MainActivity

MainActivity

This sample demonstrates the use of the MediaRouter API to show content on a secondary display using a {@link android.app.Presentation}.

The activity uses the {@link android.media.MediaRouter} API to automatically detect when a presentation display is available and to allow the user to control the media routes using a menu item provided by the {@link android.app.MediaRouteActionProvider}. When a presentation display is available a {@link android.app.Presentation} (implemented as a {@link SamplePresentation}) is shown on the preferred display. A button toggles the background color of the secondary screen to show the interaction between the primary and secondary screens.

This sample requires an HDMI or Wifi display. Alternatively, the "Simulate secondary displays" feature in Development Settings can be enabled to simulate secondary displays.

* @see android.app.Presentation @see android.media.MediaRouter

Implementing a {@link android.media.MediaRouter.Callback} to update the displayed {@link android.app.Presentation} when a route is selected, unselected or the presentation display has changed. The provided stub implementation {@link android.media.MediaRouter.SimpleCallback} is extended and only {@link android.media.MediaRouter.SimpleCallback#onRouteSelected(android.media.MediaRouter, int, android.media.MediaRouter.RouteInfo)} , {@link android.media.MediaRouter.SimpleCallback#onRouteUnselected(android.media.MediaRouter, int, android.media.MediaRouter.RouteInfo)} and {@link android.media.MediaRouter.SimpleCallback#onRoutePresentationDisplayChanged(android.media.MediaRouter, android.media.MediaRouter.RouteInfo)} are overridden to update the displayed {@link android.app.Presentation} in {@link #updatePresentation()}. These callbacks enable or disable the second screen presentation based on the routing provided by the {@link android.media.MediaRouter} for {@link android.media.MediaRouter#ROUTE_TYPE_LIVE_VIDEO} streams. @

A new route has been selected as active. Disable the current route and enable the new one.

The route has been unselected.

The route's presentation display has changed. This callback is called when the presentation has been activated, removed or its properties have changed.

Updates the displayed presentation to enable a secondary screen if it has been selected in the {@link android.media.MediaRouter} for the {@link android.media.MediaRouter#ROUTE_TYPE_LIVE_VIDEO} type. If no screen has been selected by the {@link android.media.MediaRouter}, the current screen is disabled. Otherwise a new {@link SamplePresentation} is initialized and shown on the secondary screen.

Inflates the ActionBar or options menu. The menu file defines an item for the {@link android.app.MediaRouteActionProvider}, which is registered here for all live video devices using {@link android.media.MediaRouter#ROUTE_TYPE_LIVE_VIDEO}.

Listens for dismissal of the {@link SamplePresentation} and removes its reference.

Displays the next color on the secondary screen if it is activate.