This codelab will teach you the fundamentals of building a receiver application that takes advantage of the new live features in Cast.

What is Google Cast?

Google Cast allows users to cast content from a mobile device to a TV. Users can then use their mobile device as a remote control for media playback on the TV.

The Google Cast SDK lets you extend your app to control a TV or sound system. The Cast SDK allows you to add the necessary UI components based on the Google Cast Design Checklist.

The Google Cast Design Checklist is provided to make the Cast user experience simple and predictable across all supported platforms.

What are we going to be building?

When you have completed this codelab, you will have built a Cast Receiver that takes advantage of the new Live functionality.

What you'll learn

What you'll need


How will you use this tutorial?

Read it through only Read it and complete the exercises

How would you rate your experience with building web apps?

Novice Intermediate Proficient

You can download all the sample code to your computer...

Download Source Code

and unpack the downloaded zip file.

To be able to use your receiver with a Cast device it needs to be hosted somewhere where your Cast device can reach it. Should you already have a server available to you that supports https, just skip the following instructions, just remember the URL, you'll need it in the next section.

If you don't have any server available to you, don't fret. You may install node.js, the http-server and ngrok node module.

npm install -g http-server
npm install -g ngrok

Run the server

If you're using http-server, go to your console, and do the following:

cd app-done

You should then see something like the following:

Starting up http-server, serving ./
Available on:
Hit CTRL-C to stop the server

Notice the local port used and do the following in a new terminal to expose your local receiver over HTTPS using ngrok:

ngrok http 8080

This will setup an ngrok tunnel to your local HTTP server, assigning you a globally available HTTPS secured endpoint you can use in the next step (

ngrok by @inconshreveable                                                                                                                                                                                                                                     (Ctrl+C to quit)

Session Status         online
Version                2.2.4
Web Interface
Forwarding    -> localhost:8080
Forwarding    -> localhost:8080

You should keep both ngrok and http-server running for the duration of the codelab. Any changes you make locally will be instantly available.

You must register your application to be able to run a custom receiver, as built in this codelab, on Chromecast devices. After you've registered your application, you'll receive an application ID that your sender application must use to perform API calls, such as to launch a receiver application.

Click "Add new application"

Select "Custom Receiver", this is what we're building.

Enter the details of your new receiver, be sure to use the URL you ended up with

in the last section. Make a note of the Application ID assigned to your brand new receiver.

You must also register your Google Cast device so that it may access your receiver application before you publish it. Once you publish your receiver application, it will be available to all Google Cast devices. For the purpose of this codelab it's advised to work with an unpublished receiver application.

Click on "Add new Device"

Enter the serial number printed on the back of your Cast device and give it a descriptive name. You can also find your serial number by casting your screen in Chrome when accessing Google Cast SDK Developer Console

It will take 5-15 minutes for your receiver and device to be ready for testing. After waiting 5-15 minutes you must reboot your Cast device.

Before starting this codelab, it may be helpful to review the ads developer guide which provides an overview of the new ads functionality.

We need to add support for Google Cast to the start app you downloaded. Here are some Google Cast terminology that we will be using in this codelab:

Now you're ready to build on top of the starter project using your favorite text editor:

  1. Select the android_studio_folder.pngapp-start directory from your sample code download.
  2. Open up js/receiver.js and index.html

Note, as you're working through this codelab, http-server should be picking up changes you make. If you notice it doesn't, try killing and restarting http-server.

For our sender, we will use the CAF Receiver Debugging Tool to initiate a Cast session. The receiver is designed to automatically start playing a stream.

App Design

The receiver app initializes the Cast session and will stand-by until a LOAD request (ie. the command to playback a piece of media) from a sender arrives.

The app consists of one main view, defined in index.html and one JavaScript file called js/receiver.js containing all the logic to make our receiver work.


This html file will contain all of the UI for our receiver app. For now it's basically empty.


This script manages will manage all of the logic for our receiver app. Right now it contains a basic CAF receiver.

To begin, open the web sender in Chrome. Enter the Receiver Application ID you were given on the Cast SDK Developer Console and click ‘Set''.

In the receiver we need to add some logic to include ads in the content.

Copy the following line into your js/receiver.js file. It contains a sample VMAP tag link from DoubleClick plus some randomization.

var vmapUrl = "" + Math.floor(Math.random() * Math.pow(10, 10));

In your js/receicer.js file, locate the playerManager.setMessageInterceptor function and add the following before the last return request; line in the function. = {
    adTagUrl: vmapUrl,

Note: The object assigned to vmapAdsRequest above is a shorthand version of of a VastAdsRequest object.

Save your changes to js/receiver.js and initiate a Cast session on the web sender by right clicking anywhere on the page and selecting ‘Cast'. The ads stream should begin playing immediately.

CAF has a new class called BreakManager which assists you with implementing custom business rules for ad behaviors. Let's assume that you want to enable customers a grace period to skip ads after a certain time.

The sender in our example does not have media controls. Let's add a start offset of 10 seconds so the stream begins playback after the pre-roll, but before the first mid-roll Break at the 15-second mark.

Find the playerManager.setMessageInterceptor and add the following line before the return request.

request.currentTime = 10;

Save the receiver.js file and initiate a Cast session. You should see the content load 10 seconds into it, and then play an ad 5 seconds later.

Now let's add a rule to skip the mid-roll at the 15-second mark.

You'll need an instance of the BreakManager to set an interceptor for the break loading. Copy the following line into your js/receiver.js file, after the lines containing the context and playerManager variables.

const breakManager = playerManager.getBreakManager();

Now, let's set up an interceptor with a rule to ignore any ad breaks that occurs before 60 seconds. This interceptor works like the LOAD interceptor on the PlayerManager, except this one is specific to loading BreakClips.

Copy the following into your js/receiver.js file.

breakManager.setBreakClipLoadInterceptor((breakClip, breakCtx) => {
  let break = breakCtx.break;
  if(break.position < 60)
    return null;
    return breakClip;

Note: We return null here for the BreakClips that should be skipped.

Save your changes to js/receiver.js and initiate a Cast session on the web sender by right clicking anywhere on the page and selecting ‘Cast'.

The stream should begin playback but the ad block we previously saw at 15-seconds will be skipped.

You now know how to add ads to your receiver application using the latest Cast Receiver SDK.

Take a look at our sample apps on GitHub: