window.chrome.cast not available on Android Chrome browser

Viewed 455

Background

In order to initialize a Chromecast session from my video player, I need to call a method on window.chrome.cast. This works correctly on desktop Chrome, however Android Chrome's window.chrome object does not include the requisite cast field.

I am using a Google Pixel running Android 9. Your advice is appreciated!

The code

Before initializing the Chromecast session, I add a listener on the window that detects whether the Google Chromecast API is available:

window.__onGCastApiAvailable = (isAvailable) => {
    if (isAvailable) {
        this.initializeCastApi();
    }
};

On Android Chrome, isAvailable does not become true and thus the API is never initialized. If I remove the conditional and just run initializeCastApi(), the following line is the first to run in that method:

const sessionRequest = new chrome.cast.SessionRequest(/* App ID */);

On Android, this causes the following error, since window.chrome.cast does not exist:

Uncaught TypeError: Cannot read property 'SessionRequest' of undefined

Expectations vs reality

I expect the Chromecast behavior on Android Chrome to function as it does on desktop Chrome, since the Chromecast API is available on both.

However the window.chrome objects are not the same, and since Android's object does not include cast, I am unable to launch Chromecast on that browser.

Screenshots

The window.chrome object on desktop Chrome (notice the presence of cast): enter image description here

And the window.chrome object on Android Chrome: enter image description here

1 Answers

The Presentation API which contains Chromecast isn't available over HTTP anymore.

Try loading your web sender over a HTTPS connection.

The easiest way to do that is by using ngrok;

  1. Install ngrok
  2. Run ngrok http 3000 (replace 3000 with your local port)
  3. Navigate to the forwarding address with https displayed in the terminal
Related