0

Our site uses ajax to navigate pages and that's making it hard to request a new session when the page changes. Ideally when a user changes pages, I'd like to stop the currently playing session and start a new session with the video that's on the new page.

On the first pageload, I append the https://www.gstatic.com/cv/js/sender/v1/cast_sender.js script, call the init method:

var sessionRequest = new chrome.cast.SessionRequest(applicationID);
var apiConfig = new chrome.cast.ApiConfig(sessionRequest,
this.sessionListener.bind(this),
this.receiverListener.bind(this));
chrome.cast.initialize(apiConfig, this.onInitSuccess.bind(this), this.onError.bind(this));

everything works fine, my callbacks are called and I can start the chromecast session.

On secondary page loads, I don't re-add the cast_sender.js script. When I call the initalize method and the sessionListener callback doesn't execute. If I try holding on to the session between pages, I can access the session and get to it's media object, but if I try calling any methods on the session or media object, I just get back an error:

TypeError: Cannot call method 'postMessage' of null

Seems like there's some magic binding that happens when executing the cast_sender.js script that I'm missing?

Jgubman
  • 4,948
  • 1
  • 14
  • 8
  • Have you played with the AutoJoinPolicy? https://developers.google.com/cast/docs/reference/chrome/chrome.cast.AutoJoinPolicy – ssgg Google Developer Platform Feb 10 '14 at 23:36
  • Yeah, tried playing with both the AutoJoinPolicy and DefaultActionPolicy and neither seem to have any effect. – Jgubman Feb 11 '14 at 16:49
  • Your secondary page is loaded via ajax call? If so, try to load the cast_sender.js asynchronously. See how to do this http://stackoverflow.com/questions/8097026/how-to-run-javascript-in-html-loaded-via-ajax – ssgg Google Developer Platform Feb 13 '14 at 08:28
  • Yeah, when I load async things work, but the listeners start piling up. So on the second pageview I get two notifications from various callbacks (callbacks I created and mystery ones in the chromecast library), on third pageview, I get three notifications etc. – Jgubman Feb 14 '14 at 00:18

1 Answers1

0

We've just released the Google Cast extension Beta that potentially addresses this issue. See my post: https://plus.google.com/+ShawnShen/posts/aVXSHyceNbR

You may add something like the following in your app to do both sync/async script loading.

window['_onGCastApiAvailable'] = (function(loaded, errorInfo) { if (loaded) { this.init(); } else { this.appendMessage_(errorInfo); } }).bind(this);