Using Chrome Cast Java API in your grails app

Posted By : Shakil Pathan | 29-Dec-2014

In this blog, I am going to explain how to connect your app to chromecast devices using java/groovy code.

Google Cast is a technology that enables multi-screen experiences and lets a user send and control content like video from a small computing device like a phone, tablet, or laptop to a large display device like a television.

 

The sender may be a phone or tablet running on Android or iOS, or it may be a laptop computer running Chrome.

So if you want to connect your app to chromecast you need Android, iOS or a laptop computer running Chrome.

 

But what if you want to throw your videos using your other browser like firefox, So you have to communicate using your server (java code).

 

 

Here I am going to explain you how to make connection to chromecast and play your videos on the chromecast.

I have used a third party implementation of Google ChromeCast V2 protocol in java.

 

For using the API you have to paste the below line to your BuildConfig.groovy.

dependencies {
// ...
    runtime 'su.litvak.chromecast:api-v2:0.0.5'
// ...
}

After you can use this code to search chromecast in your LAN.

ChromeCasts.startDiscovery();

And can get any device like:

ChromeCast chromecast = ChromeCasts.get().get(0);

or using each closure in groovy. And perform any action like:

chromecast.connect();
// Get device status
Status status = chromecast.getStatus();
// Run application if it's not already running
if (chromecast.isAppAvailable("APP_ID") && !status.isAppRunning("APP_ID")) {
  Application app = chromecast.launchApp("APP_ID");
}

Please note that you need an Chromecast app id to load into your chromecast device. For chromecast receiver app please refer:

https://developers.google.com/cast/docs/receiver_apps

And load any video from url like:

// play media URL directly
chromecast.load("http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4");
// play media URL with additional parameters, such as media title and thumbnail image
chromecast.load("Big Buck Bunny",           // Media title
                "images/BigBuckBunny.jpg",  // URL to thumbnail based on media URL
                "http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4", // media URL
                "video/mp4" // media content type
                );

For more details of the api, please refer:

https://github.com/vitalidze/chromecast-java-api-v2

 

 

Thanks

Shakil

About Author

Author Image
Shakil Pathan

Shakil is an experienced Groovy and Grails developer . He has also worked extensively on developing STB applications using NetGem .

Request for Proposal

Name is required

Comment is required

Sending message..