Top Banner
Developing for Chromecast Cast Companion Library & Custom Receiver Application
33

Developing for Chromecast on Android

Apr 12, 2017

Download

Mobile

Kurt Mbanje
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Developing for Chromecast on Android

Developing for ChromecastCast Companion Library & Custom Receiver Application

Page 2: Developing for Chromecast on Android

Kurt Mbanje@ckurtm / peirr.com

Page 3: Developing for Chromecast on Android

What is Chromecast?

Page 4: Developing for Chromecast on Android
Page 5: Developing for Chromecast on Android
Page 6: Developing for Chromecast on Android

How does it work?

Page 7: Developing for Chromecast on Android

• Sender application

Two componentsApplications running on a mobile device or laptop

• Receiver application(HTML, JavaScript , CSS) app running on a Chromecast or other cast compatible device.

Page 8: Developing for Chromecast on Android

• Sender application

Two componentsApplications running on a mobile device or laptop

• Receiver application(HTML, JavaScript , CSS) app running on a Chromecast or other cast compatible device.

Page 9: Developing for Chromecast on Android

Receiver application

Page 10: Developing for Chromecast on Android
Page 11: Developing for Chromecast on Android
Page 12: Developing for Chromecast on Android

• Custom Media Receiver

3 Types of receivers

Requires application id , DRM Content, Data centric apps

• Styled Media ReceiverCan be styled with CSS files, requires application id

• Default Media ReceiverDoes not require application id, can use CastMediaControlIntent.DEFAULT_MEDIA_RECEIVER_APPLICATION_ID in your sender if using CCL

Page 13: Developing for Chromecast on Android

Custom Receiver

Page 14: Developing for Chromecast on Android

www.gstatic.com/cast/sdk/libs/receiver/2.0.0/cast_receiver.js

Receiver API

Page 15: Developing for Chromecast on Android

Simple Custom Receiver

<head> <title>LocalCast</title> <script src="//www.gstatic.com/cast/sdk/libs/receiver/2.0.0/cast_receiver.js"></script> <link rel="stylesheet" href="receiver.css"/></head>

Page 16: Developing for Chromecast on Android

<script> //[3] handle the payload from the sender function process(json){ console.log('received: ' + json.url); document.getElementById("image").src= json.url; } //[1] get a handle to the receiver manager window.castReceiverManager = cast.receiver.CastReceiverManager.getInstance(); castReceiverManager.onSenderDisconnected = function(event) { console.log('disconnected: ' + event.data); if (window.castReceiverManager.getSenders().length == 0) { //close the app if we have no more connected devices window.close(); } }; //[2] create a CastMessageBus to handle messages for a custom namespace window.messageBus = window.castReceiverManager.getCastMessageBus('urn:x-cast:com.peirr.localcast'); window.messageBus.onMessage = function(event) { var json = JSON.parse(event['data']); //decode the request from sender app window.sender = event.senderId; process(json); } //[4]start the listener window.castReceiverManager.start(); </script>

Page 17: Developing for Chromecast on Android

Publishing Receiver Application

Page 18: Developing for Chromecast on Android

• Development• Publish to any server & add endpoint to cast

console• Debug http://RECEIVER-IP-ADDRESS:9222

• ProductionServer has to be secure (https://<yourserver>)

• Google Cast SDK Developer Consolehttps://cast.google.com/publish/

Publishing

Page 19: Developing for Chromecast on Android

• Development• Publish to any server & add endpoint to cast

console• Debug http://RECEIVER-IP-ADDRESS:9222

• ProductionServer has to be secure (https://<yourserver>)

• Google Cast SDK Developer Consolehttps://cast.google.com/publish/

Publishing

Page 20: Developing for Chromecast on Android

• Development• Publish to any server & add endpoint to cast

console• Debug http://RECEIVER-IP-ADDRESS:9222

• ProductionServer has to be secure (https://<yourserver>)

• Google Cast SDK Developer Consolehttps://cast.google.com/publish/

Publishing

Page 21: Developing for Chromecast on Android

Sender Application

Page 22: Developing for Chromecast on Android
Page 23: Developing for Chromecast on Android

Android development options

Page 24: Developing for Chromecast on Android

compile "com.android.support:mediarouter-v7:23.3.0"compile 'com.google.android.gms:play-services-cast:8.4.0'

Page 25: Developing for Chromecast on Android

compile "com.android.support:mediarouter-v7:23.3.0"compile 'com.google.android.gms:play-services-cast:8.4.0'

compile 'com.google.android.libraries.cast.companionlibrary:ccl:2.8.3'

Page 26: Developing for Chromecast on Android

Cast Companion Library

Page 27: Developing for Chromecast on Android

Cast Companion Libraryhttps://github.com/googlecast/CastCompanionLibrary-android

Page 28: Developing for Chromecast on Android

Example: LocalCasthttps://github.com/ckurtm/LocalCast

Page 29: Developing for Chromecast on Android
Page 30: Developing for Chromecast on Android

Initialize

CastConfiguration options =new CastConfiguration.Builder("84B70D9D") .enableAutoReconnect() .enableDebug() .build();DataCastManager.initialize(this,options);

Page 31: Developing for Chromecast on Android

Cast Button<item android:id="@+id/action_cast" android:title="@string/action_cast" app:actionProviderClass="android.support.v7.app.MediaRouteActionProvider" app:showAsAction="always"/>

@Overridepublic boolean onCreateOptionsMenu(Menu menu){ super.onCreateOptionsMenu(menu); getMenuInflater().inflate(R.menu.menu,menu); DataCastManager.getInstance().addMediaRouterButton(menu,R.id.action_cast); return true;}

Page 32: Developing for Chromecast on Android

Send data to Receiver Application

JSONObject obj = new JSONObject();String url = "http://"+ info.ip + ":" + info.port + "/" + item.getFile().getAbsolutePath();Log.d(TAG, "casting: " + url);try { obj.put("url",url); DataCastManager.getInstance().sendDataMessage(obj.toString(),"urn:x-cast:com.peirr.localcast");} catch (JSONException|IOException e) { e.printStackTrace();}

Page 33: Developing for Chromecast on Android

Questions