Chromecast - DevParty Torino 2014

Post on 31-May-2015

256 views 0 download

description

Chromecast by Google - Gianluca Arbezzano @ CorleyCloud

Transcript of Chromecast - DevParty Torino 2014

DEVPARTYGOOGLE CHROMECAST AND ANGULARJS

Created by / Gianluca Arbezzano @gianarb

WHAT IS THIS?!

Chromecast is a thumb-sized media streaming device that plugsinto the HDMI port on your TV

SETUPCHROMECAST.COM/SETUP

DEVELOPER PROGRAMDEVELOPERS.GOOGLE.COM/CAST

SENDERGOOGLE CAST SUPPORTS SENDER APPLICATIONS WRITTEN

FOR THE ANDROID, IOS, AND CHROME PLATFORMS.

RECEIVERGOOGLE CAST SUPPORTS SENDER APPLICATIONS WRITTEN

FOR THE ANDROID, IOS, AND CHROME PLATFORMS.

MY CHROMECAST GENERIC SENDER

sendev-cast.gianarb.it/#/home

.run(['$window', '$timeout', '$rootScope', '$q', function run ($window, $timeout, $rootScope, $q) { /** * Manage history */ $rootScope.logs = []; /** * Cast is ready?! */ if (!$window.chrome.cast || ! $window.chrome.cast.isAvailable) { var def = $q.defer(); def.resolve(chrome.cast); $rootScope.cast = def.promise; }}])

$rootScope.cast.then(function(cast){ $window.sessionRequest = new chrome.cast.SessionRequest(appId); $rootScope.configuration.appId = appId; var apiConfig = new chrome.cast.ApiConfig( $window.sessionRequest, $window.sessionListener, $window.receiverListener ); cast.initialize( apiConfig, function(event){ console.log("Good!"); }, function(event){ if(event){ console.log("D'oh"); } } ); });

$scope.cast = function(){ $rootScope.cast.then(function(cast){ cast.requestSession(function(e){ $window.session = e; }, function(){ console.log("ERROR"); }); });};

WORK WITH BUS $window.session.sendMessage( namespace, message, function(e){ console.log("OK"); }, function(e){ console.log("error"); });

WORK WITH MEDIA $scope.loadMedia = function(url){ var onMediaDiscovered = function(how, media) { console.log("new media session ID:" + media.mediaSessionId); $scope.currentMedia = media; };

$rootScope.cast.then(function(cast){ var mediaInfo = new cast.media.MediaInfo(url); mediaInfo.contentType = 'video/mp4'; var request = new chrome.cast.media.LoadRequest(mediaInfo); request.autoplay = true; request.currentTime = 0; session.loadMedia(request, onMediaDiscovered.bind(this, 'loadMedia'), function(e){ $rootScope.logs.push({ message: e, type: "error" }); } ); }); };

BUS RECEIVERvar handlers = { onReady: function(event) { $rootScope.$broadcast("$castOnReady", event); session.setApplicationState("Application status is ready..."); }, onSenderConnected: function(event) { statusConnect = true; $rootScope.$broadcast("$castOnSenderConnected", event); }, onSenderDisconnected: function(event) { $rootScope.$broadcast("$castOnSenderDisconnected", event); if (session.getSenders().length === 0) { window.close(); } }, onSystemVolumeChanged: function(event) { $rootScope.$broadcast("$castOnSystemVolumeChanged", event); }, onMessage: function(event) { $rootScope.$broadcast("$cast-urn:x-cast:com.corley.google.cast", event); messageBus.send(event.senderId, event.data); }};

session = cast.receiver.CastReceiverManager.getInstance();session.onSenderDisconnected = handlers.onSenderDisconnected;session.onSenderConnected = handlers.onSenderConnected;session.onSystemVolumeChanged = handlers.onSystemVolumeChanged;session.onReady = handlers.onReady;

messageBus = session.getCastMessageBus('urn:x-cast:com.corley.google.cast');messageBus.onMessage = handlers.onMessage;

THANKS AND GOOD PATY