Skip to content

Chromecast API v2 Resources

When you want to cast content from The Radio Hub to a Chromecast connected to a tv, you can use the The Radio Hub Chromecast Receiver. You can connect to our receiver and send commands to play a stream or start our crossfading youtube player.

Create a Sender Application

Connect to a Chromecast and load the The Radio Hub Application before doing anything else. Use the libraries provided by Google to do that. See https://developers.google.com/cast/docs/sender_apps for tips and requirements to get you started. Examples on this page use the javascript syntax, but the documentation from Google mentions other languages as well.

Parameters

ParameterRequiredDescription
application idRequiredThe id of the Custom Receiver Application from The Radio Hub is 0FBEED1A

Successfull connection

Once you are connected to a Chromecast, a The Radio Hub loading screen will show. Now you can send commands.

Stream & Nowplaying info

Plays the radio stations' audio while showing the now playing information on screen.

Steps

  1. Create a mediainfo object, telling the Chromecast player to play a stream.
js
const media = new chrome.cast.media.MediaInfo('playStream', 'text/plain');
Advanced mode: set your own stream url
  1. Do not send playStream as plain text, but create a mediainfo object with the stream url you want to play.
js
const media = new chrome.cast.media.MediaInfo('stream url', 'audio/mp3');
  1. Create a Loadrequest object with the media in it:
js
const load = new chrome.cast.media.LoadRequest(media);
  1. Add the station id in the custom data:
js
load.customData = {
    stationId: 53 // Radio Haugaland
}
  1. Send the request to the Chromecast
js
const currentSession = this.getCurrentSession();
currentSession.loadMedia(load).then(
    () => { console.log('Load succeed'); },
    (errorCode) => { console.log('Error code: ' + errorCode); });

Videoplaylist

Shows a fullscreen videoplaylist, with radio station branding. This call allows you to play a list of youtube video's in a crossfading player.

Steps

  1. Create a mediainfo object with the stream url you want to play.
js
const media = new chrome.cast.media.MediaInfo('the youtube id of the first video', 'video/youtube');
  1. Create a Loadrequest object with the media in it:
js
const load = new chrome.cast.media.LoadRequest(media);
  1. Create a queue with all the videos you want to play (including the first track).
js
const queue = new chrome.cast.media.QueueData();
queue.items = tracks.map((track) => {
    const mediainfo = new chrome.cast.media.MediaInfo(track.youtube_id, 'video/youtube');
    mediainfo.metadata = new chrome.cast.media.GenericMediaMetadata();
    mediainfo.metadata.title = track.title;
    mediainfo.metadata.artist = track.artist;
    return new chrome.cast.media.QueueItem(mediainfo);
});
load.queueData = queue;
  1. Add the station id in the custom data:
js
load.customData = {
    stationId: 53 // Radio Haugaland
}
  1. Send the request to the Chromecast
js
const currentSession = this.getCurrentSession();
currentSession.loadMedia(load).then(
    () => { console.log('Load succeed'); },
    (errorCode) => { console.log('Error code: ' + errorCode); });

Userlikes videoplaylist

Shows a fullscreen videoplaylist with songs the user liked, with radio station branding. The playlist will be mixed with songs from the radio station.

  1. Create a mediainfo object
js
const media = new chrome.cast.media.MediaInfo('playUserLikes', 'text/plain');
  1. Create a Loadrequest object with the media in it:
js
const load = new chrome.cast.media.LoadRequest(media);
  1. Add the station id and user info in the custom data:
js
load.customData = {
    stationId: 53, // Radio Haugaland
    action: 'playUserLikes',
    type: 'user',
    typeId: 9 // user-id
}
  1. Send the request to the Chromecast
js
const currentSession = this.getCurrentSession();
currentSession.loadMedia(load).then(
    () => { console.log('Load succeed'); },
    (errorCode) => { console.log('Error code: ' + errorCode); });