Adding the Cast Button to your Android app protip
If you want to share content with a TV, then the Google Cast SDK makes that easy to do. Google Cast apps use Android’s MediaRouter to discover and connect to Google Cast devices such as Chromecast. All Cast apps need to have a visible Cast button on every screen.
There are 3 options for adding a Cast button to your app:
- Use the MediaRouterActionProvider
- the easiest and recommended way of adding a Cast button. The MediaRouter will handle the entire lifecycle of the button including the visibility of the button when devices are discovered. You need to use the appcompat ActionBar and extend ActionBarActivity for your Activities.
- Use the MediaRouteButton
. You need to extend FragmentActivity for your Activities. Your app is responsible for managing the visibility of the Cast button based on the MediaRouter device discovery event callbacks.
- Use a custom button
. You are responsible for making the custom button behave like the MediaRouter Cast button, including the dialogs for selecting a device and controlling a selected device. This option only makes sense if you cannot use the appcompat ActionBar.
Once the Cast button is added to your app, you need to support the rest of the Cast app lifecycle to display content on the Google Cast device: http://goo.gl/zUiYC4
Note: It is important that you use the v7-mediarouter support library package and not the MediaRouter classes in the Android framework. You can learn more about the MediaRouter here: http://goo.gl/qNBkCr
Take a look at sample code we have open sourced for adding a Cast button: http://goo.gl/JwK1wJ
Happy Casting! #googlecastsdk #chromecast #android #protip