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?