Android SDK Chromecast

Adding support for Chromecast

In order to enable chromecast, a few extra steps need and can be taken. These steps are described below.

1 Steps to enable Chromecast:

  1. Upgrade to the latest THEOplayer Android SDK release (≥ 1.0.9)
  2. To add chromecast support the first thing you need is to include the Cast Framework and Media Router libraries under the dependencies node in your build.gradle file.
    compile 'com.android.support:mediarouter-v7:25.3.1'
    compile 'com.google.android.gms:play-services-cast-framework:10.2.1'
  3. To enable chromecast on android a CastOptionsProvider should be specified. This class contains the configuration for chromecast on Android. A DefaultCastOptionsProvider is provided as part of the THEOplayer Android SDK. This OptionsProvider associates the THEOplayer chromecast receiver with your sender application. You will need to register this class in your AndroidManifest.xml file within a meta-data tag under your application node.
    <meta-data
            android:name="com.google.android.gms.cast.framework.OPTIONS_PROVIDER_CLASS_NAME"
            android:value="com.theoplayer.android.api.cast.chromecast.DefaultCastOptionsProvider"/>
    
    If you need custom settings for your chromecast (for example a custom receiver id) you can create your own CastOptionsProvider by subclassing com.google.android.gms.cast.framework.OptionsProvider and implementing the getCastOptions method.
    @Override
    public CastOptions getCastOptions(Context context) {
            return new CastOptions.Builder()
                .setReceiverApplicationId(DEFAULT_APP_ID)
                .build();
    }

2 Optional extra steps:

2.1 Add routing button to your app:

In order to get this button to appear in your application's Toolbar, the easiest way is to include it in the menu XML file for your Activity.

  1. In your res folder add a menu folder
  2. Add a activity_main.menu.xml file
  3. Add the following contents to this file
    <?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto">
        <item
        android:id="@+id/media_route_menu_item"
        android:title="Cast"
        app:actionProviderClass="android.support.v7.app.MediaRouteActionProvider"
        app:showAsAction="always" />
    </menu>
  4. Initialize this new MenuItem in the onCreateOptionsMenu method of your Activity (Make sure you have an AppCompatActivity).
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
            super.onCreateOptionsMenu(menu);
            getMenuInflater().inflate(R.menu.activity_main_menu, menu);
            CastButtonFactory.setUpMediaRouteButton(getApplicationContext(),
                menu, R.id.media_route_menu_item);
            return true;
    }
    

2.2 Add MiniController to your app:

  1. In your main activity xml add the following fragment
    <fragment
            android:id="@+id/castMiniController"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:visibility="gone"
            class="com.google.android.gms.cast.framework.media.widget.MiniControllerFragment" />