Try to keep the same Cast session

This commit is contained in:
Pierre Rudloff 2014-04-04 23:57:41 +02:00
parent aab3933275
commit 0f41448ee4

77
cast.js
View file

@ -3,19 +3,39 @@
var launchBtn, disabledBtn, stopBtn; var launchBtn, disabledBtn, stopBtn;
var session, currentMedia; var session, currentMedia;
function receiverListener() { function receiverListener(e) {
'use strict'; 'use strict';
console.log('receiverListener'); console.log('receiverListener', e);
}
function sessionListener() {
'use strict';
console.log('sessionListener');
} }
function onMediaDiscovered(how, media) { function onMediaDiscovered(how, media) {
'use strict'; 'use strict';
console.log('onMediaDiscovered', how);
currentMedia = media; currentMedia = media;
if (launchBtn) {
stopBtn.classList.remove('cast_hidden');
launchBtn.classList.add('cast_hidden');
}
}
function sessionListener(e) {
'use strict';
session = e;
session.addMediaListener(onMediaDiscovered.bind(this, 'addMediaListener'));
if (session.media.length !== 0) {
onMediaDiscovered('onRequestSessionSuccess', session.media[0]);
}
}
function onStopCast() {
'use strict';
stopBtn.classList.add('cast_hidden');
launchBtn.classList.remove('cast_hidden');
}
function stopCast() {
'use strict';
session.stop(onStopCast);
} }
function onMediaError() { function onMediaError() {
@ -28,44 +48,20 @@ function onRequestSessionSuccess(e) {
'use strict'; 'use strict';
session = e; session = e;
var videoLink = document.getElementById('video_link'), videoURL = videoLink.dataset.video, mediaInfo = new chrome.cast.media.MediaInfo(videoURL, 'video/' + videoLink.dataset.ext), request = new chrome.cast.media.LoadRequest(mediaInfo); var videoLink = document.getElementById('video_link'), videoURL = videoLink.dataset.video, mediaInfo = new chrome.cast.media.MediaInfo(videoURL, 'video/' + videoLink.dataset.ext), request = new chrome.cast.media.LoadRequest(mediaInfo);
stopBtn.classList.remove('cast_hidden');
launchBtn.classList.add('cast_hidden');
session.loadMedia(request, onMediaDiscovered.bind(this, 'loadMedia'), onMediaError); session.loadMedia(request, onMediaDiscovered.bind(this, 'loadMedia'), onMediaError);
} }
function onLaunchError() { function onLaunchError(e) {
'use strict'; 'use strict';
console.log('onLaunchError'); console.log('onLaunchError', e.description);
}
function onInitSuccess() {
'use strict';
chrome.cast.requestSession(onRequestSessionSuccess, onLaunchError);
}
function onError() {
'use strict';
console.log('onError');
}
function onStopCast() {
'use strict';
stopBtn.classList.add('cast_hidden');
launchBtn.classList.remove('cast_hidden');
} }
function launchCast() { function launchCast() {
'use strict'; 'use strict';
var sessionRequest = new chrome.cast.SessionRequest(chrome.cast.media.DEFAULT_MEDIA_RECEIVER_APP_ID), apiConfig = new chrome.cast.ApiConfig(sessionRequest, sessionListener, receiverListener, chrome.cast.AutoJoinPolicy.PAGE_SCOPED); chrome.cast.requestSession(onRequestSessionSuccess, onLaunchError);
chrome.cast.initialize(apiConfig, onInitSuccess, onError);
} }
function stopCast() { function onInitSuccess() {
'use strict';
session.stop(onStopCast);
}
function initializeCastApi() {
'use strict'; 'use strict';
launchBtn = document.getElementById('cast_btn_launch'); launchBtn = document.getElementById('cast_btn_launch');
disabledBtn = document.getElementById('cast_disabled'); disabledBtn = document.getElementById('cast_disabled');
@ -78,6 +74,17 @@ function initializeCastApi() {
} }
} }
function onError() {
'use strict';
console.log('onError');
}
function initializeCastApi() {
'use strict';
var sessionRequest = new chrome.cast.SessionRequest(chrome.cast.media.DEFAULT_MEDIA_RECEIVER_APP_ID), apiConfig = new chrome.cast.ApiConfig(sessionRequest, sessionListener, receiverListener, chrome.cast.AutoJoinPolicy.ORIGIN_SCOPED);
chrome.cast.initialize(apiConfig, onInitSuccess, onError);
}
function loadCastApi(loaded, errorInfo) { function loadCastApi(loaded, errorInfo) {
'use strict'; 'use strict';
if (loaded) { if (loaded) {