Enable casting to Chromecast devices

Learn how to enable casting with the Android SDK.


The Google Cast framework enables a viewer to stream video and audio content to a compatible TV or sound system. By including the Google Cast framework in your app, a viewer can use a cast button to stream your content to a Chromecast-enabled device on a shared network connection.

👍

IMPORTANT

• The JW Player SDK supports casting to the Default Media Receiver and to Styled Media Receivers.

• Custom receivers are not officially supported. However, if the video playback implements the same interface used in the Default Media Receiver, you may be able to initiate a casting session with a custom receiver.

• To specify a receiver, specify a media receiver app ID when initializing the CastManager.

The following sections explain how to enable the Google Cast framework for your Android app:

  • Add SDK dependencies for Google Cast
  • Configure your app for Google Cast

After completing the steps in each section, a viewer will be able to begin a casting session from your app.



Add SDK dependency for Google Cast

To use the Google Cast framework, you must add a dependency to your app. You can use Maven or manually add the dependency.

Use Maven

  1. In Android Studio, open the build.gradle file for your app.
  2. Add the com.longtailvideo.jwplayer:jwplayer-chromecast:x.x.x dependency. Be sure the version number of the module (x.x.x) matches the version number you use for the jwplayer-core and jwplayer-common dependencies.
  3. Sync Gradle.
dependencies {
    ...
    implementation 'com.longtailvideo.jwplayer:jwplayer-chromecast:x.x.x'
}

Manually add the .aar dependency

  1. In Android Studio, open your app.
  2. Click File > New > New Module... > Import .JAR / .AAR Package.
  3. Click Next.
  4. Select jwplayer-chromecast:x.x.x.aar from your computer.
  5. Click Finish.
  6. Click File > Project Structure... > Modules > App > Dependencies.
  7. Click the plus sign in the main panel.
  8. Select Module dependency.
  9. Select jwplayer-chromecast:x.x.x.
  10. Click OK.
  11. Include the Google Cast SDK in your app's build.gradle file
dependencies {
...
implementation 'com.google.android.gms:play-services-cast-framework:19.0.0'
}


Configure your app for Google Cast

Now that you have added the Google Cast dependency, you must configure your app:

  1. Implement the OptionsProvider interface. This interface supplies options needed to initialize CastContext. CastContext is a global singleton object that coordinates all interactions of the framework.

    This interface creates an instance of LaunchOptions that defines how the receiver application is launched. For example, setLanguage() allows you to set the language to be used by the receiver application.

    This interface also creates an instance of CastOptions that defines the behavior of the framework. For example, setReceiverApplicationId() allows you to filter discovery results and to launch the receiver app when a cast session starts.
public class CastOptionsProvider implements OptionsProvider {

    @Override
    public CastOptions getCastOptions(Context context) {
        LaunchOptions launchOptions = new LaunchOptions.Builder()
            .setLocale(Locale.US)
           .build();
            
        CastOptions castOptions = new CastOptions.Builder()
            .setReceiverApplicationId(context.getString(R.string.app_id))
            .setLaunchOptions(launchOptions);
            .build();

        return castOptions;
    }

    @Override
    public List<SessionProvider> getAdditionalSessionProviders(Context context) {
        return null;
    }
}
  1. In the AndroidManifest.xml of the sender app, use a <meta-data/> element to declare the fully-qualified name of the implemented OptionsProvider.
<application>
    ...
    <meta-data
        android:name="OPTIONS_PROVIDER_CLASS_NAME"
        android:value="com.foo.CastOptionsProvider" />

</application>

Now that you have enabled the Google Cast framework for your Android app, a viewer can now begin casting from your app.



FAQ

Which features are not supported when casting with an Android SDK player?


Did this page help you?