An API to send YouTube videos from an Android app to a Chromecast device

This tutorial explains how to use the android-youtube-player API and its chromecast extension to play YouTube videos on a Chromecast device

YouTube doesn't offer any official API to cast YouTube videos to a Google Cast enabled device. To solve this problem I have built an API that does just that: the chromecast-sender, an extension of the android-youtube-player API.

If you don’t know what the android-youtube-player API is, you can read an introductory article here, or read the description on the homepage of the project.

Components of a Google Cast application

A Google Cast application is made of two main components: a Sender and a Receiver.

Sender: is responsible for initiating the cast sessions. In our case the sender is our Android app.

Receiver: a web app that gets downloaded on the Chromecast device when a sender initiates a sessions.

You can find a more detailed explanation on the official Google Cast documentation.

chromecast-sender, quickstart

First of all, add the library to your app’s dependencies.

In order to use the Google Cast framework an app has to declare a OptionsProvider , as described in the Google Cast documentation.

To do that just add this class to your project:

(I will explain how to get a receiverId at the end of this post)

Then add it as <meta-data> in your app’s manifest.xml. Just change the value of the android:value attribute to the name of your OptionsProvider.

Now we’re done with the basic setup and can start writing the actual logic to use the chromecast-sender API.

Add a MediaRouteButton to your layout. In your layout xml file, or programmatically.

The MediaRouteButton is quite smart, it will automatically become clickable when a Chromecast is detected on your local wifi and will update it’s UI to match the state of the connection between your device and the Chromecast.

Before using the cast framework, we need to check the the status of the GooglePlayeServices on the user's phone. You can’t use the cast framework if GooglePlayServices aren’t available.

To check if GooglePlayServices are available do the following operations in your app’s Activity:

This snippet of code uses the PlayServicesUtils.checkGooglePlayServicesAvailability (a utility function provided by the chromecast-sender library) to check the availability of the GooglePlayServices.

PlayServicesUtils.checkGooglePlayServicesAvailability does what is described here, on the official doc. It will check the status of GooglePlayServices and will show a dialog to the user if an action is needed to fix a problem. It won't display anything if everything is ok (which is 99% of the times).

If the check is successful the lambda passed as third argument will be called (initChromecast). If there are problems, the result of the operation is delivered through the onActivityResult function.

The initChromecast function creates the ChromecastYouTubePlayerContext , the entry point to the chromecast-sender API.

Once it is created, it automatically starts listening for Chromecast connection events (these events are emitted when the user connects to a Chromecast).

To react to these events we use an instance of ChromecastConnectionListener , which we passed to the constructor of ChromecastYouTubePlayerContext.

When a user clicks the MediaRouteButton a series of events will be triggered in the framework, the methods of ChromecastConnectionListener are called when these events are emitted.

It is easy to understand when each method is called, by reading their names.

Only after a Chromecast connection has been established (when onChromecastConnected is called) you can initialize the ChromecastYouTubePlayerContext .

By initializing the ChromecastYouTubePlayerContext you will start listening to events emitted by the YouTubePlayer running on the Chromecast.

(Notice that from now on it will be exactly the same as using a local YouTubePlayer from the core API)

Call ChromecastYouTubePlayerContext.initialize and pass a YouTubePlayeristener to be notified of events from the YouTubePlayer on the Chromecast.

You can also use the YouTubePlayer object to control the playback of the video, by calling method such as loadVideo , cueVideo , pause , play etc. The library will take care of communication with the remote player on the Chromecast.

To interact with the YouTubePlayer and to use the YouTubePlayerListener it is going to be useful to be familiar with the core API, you can read the detailed documentation on the project’s GitHub page. In particular you may care about the doc of YouTubePlayer and YouTubePlayerListener.

You can also find a few working in examples in the sample app, both in Java and Kotlin.

Chromecast Receiver

As mentioned at the beginning of the article, a Cast application is made of a sender and a receiver. So far we have built the sender.

I have also built a YouTube receiver that is ready to use, you can find the code here: chromecast-receiver.

As mentioned before, the receiver is a web page that gets downloaded on the Chromecast when a session is started. Therefore you will need to take this code and host it somewhere. You can use Firebase free hosting, or other services for hosting static pages.

Registration, how to get a receiverId

Before I told you I will have explained how to get a receiverId. This is what this paragraph is for.

In order to use your receiver you need a receiverId. This is the ID of your receiver app. To get a receiver ID you need to register your receiver app on the Google Cast SDK developer console, you can learn how to do it by reading the official documentation.

Remember to register a Custom Receiver, this is the type of receiver you need for this library.

After getting the receiverId remember to put it in your CastOptionsProvider.