Enable casting to Chromecast devices (Android)
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.
Be mindful of the following points:
- The JWP SDK supports casting to the Default Media Receiver and to Styled Media Receivers.
- 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
-
In Android Studio, open the build.gradle file for your app.
-
Add the
com.jwplayer:jwplayer-chromecast:4.x.x
dependency.Be sure the version number of the module (
x.x.x
) matches the version number you use for thejwplayer-core
andjwplayer-common
dependencies.dependencies { ... implementation 'com.jwplayer:jwplayer-chromecast:4.x.x' }
-
Sync Gradle.
Manually add the .aar dependency
- In Android Studio, open your app.
- Click File > New > New Module... > Import .JAR / .AAR Package.
- Click Next.
- Select jwplayer-chromecast:4.x.x.aar from your computer.
- Click Finish.
- Click File > Project Structure... > Modules > App > Dependencies.
- Click the plus sign in the main panel.
- Select Module dependency.
- Select jwplayer-chromecast:4.x.x.
- Click OK.
- 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:
-
Implement the
OptionsProvider
interface. This interface supplies options needed to initializeCastContext
.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; } }
-
In the AndroidManifest.xml of the sender app, use a
<meta-data/>
element to declare the fully-qualified name of the implementedOptionsProvider
.<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.
Send custom data to a custom receiver
Custom data can be sent to your custom receiver alongside the media item. For example, you can play DRM-protected content by sending the Widevine license URL or add a call to action by sending the userβs account info.
You can send custom data through the userInfo
property of the playlist item.
Do not use the reserved key
sources
in the JSON. The Delivery API uses this key to pass the sources of DRM media items protected by Studio DRM.
PlaylistItem.Builder playlistItemBuilder = new PlaylistItem.Builder();
playlistItemBuilder
.file(videoURL)
.title(title)
.description(description)
.userInfo(new JSONObject().put("licenseUrl", "https://drm-license-url"));
FAQs
Which features are not supported when casting with an Android SDK player?
The following features are not supported during a casting session with an Android SDK player:
- Advertising
- Multiple-audio tracks or AudioTrack switching
- Multiple qualities or quality switching
- 608/708 captions
- DVR and live streaming capabilities
Can the Chromecast dialog be customized?
To customize the appearance of the VideoMediaRouteControllerDialog
which appears after clicking the MediaRouteButton
, override the custom_media_route_controller_controls_dialog.xml layout file.
As an example, here is the custom_media_route_controller_controls_dialog.xml that use in the JWP demo app:
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright 2015 Google Inc. All rights reserved.
Licensed 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.
-->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="24dp"
android:paddingRight="24dp">
<RelativeLayout
android:id="@+id/iconContainer"
android:layout_width="68dp"
android:layout_height="68dp"
android:layout_alignParentLeft="true">
<ImageView
android:id="@+id/iconView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="false"
android:cropToPadding="false"
android:scaleType="centerCrop"
android:src="@drawable/album_art_placeholder"/>
</RelativeLayout>
<ImageView
android:id="@+id/playPauseView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="@drawable/ic_av_pause_sm_dark"/>
<ProgressBar
android:id="@+id/loadingView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/playPauseView"
android:layout_alignLeft="@+id/playPauseView"
android:layout_alignRight="@+id/playPauseView"
android:layout_alignTop="@+id/playPauseView"
android:layout_centerVertical="true"
android:visibility="gone"/>
<LinearLayout
android:id="@+id/textContainer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:layout_marginRight="10dp"
android:layout_toLeftOf="@+id/playPauseView"
android:layout_toRightOf="@+id/iconContainer"
android:orientation="vertical" >
<TextView
android:id="@+id/titleView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/iconContainer"
android:layout_marginLeft="10dp"
android:layout_marginRight="5dp"
android:layout_marginTop="6dp"
android:layout_toLeftOf="@+id/playPauseView"
android:layout_toRightOf="@+id/iconContainer"
android:ellipsize="end"
android:maxLines="1"
android:textColor="@color/ccl_mr_custom_line_1"
android:textSize="15sp"/>
<TextView
android:id="@+id/subTitleView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/iconContainer"
android:layout_marginBottom="7dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="5dp"
android:layout_toLeftOf="@+id/playPauseView"
android:layout_toRightOf="@+id/iconContainer"
android:ellipsize="end"
android:maxLines="1"
android:textColor="@color/ccl_mr_custom_line_2"
android:textSize="13sp"/>
</LinearLayout>
<TextView
android:id="@+id/emptyView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:gravity="center"
android:maxLines="1"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:text="@string/ccl_no_media_info"
android:textAlignment="center"
android:textColor="@color/ccl_mr_custom_line_1"
android:textSize="15sp"
android:visibility="gone"/>
</RelativeLayout>
Updated 4 months ago