Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I play audio to a Chromecast when casting a URL to it?

I cast URLs to my Chromecast to display web pages on it, and this generally works fine, the pages run scripts, css and images.

However I've not been able to play audio in those web pages. When I cast something like YouTube to the Chromecast that audio does play as expected.

I've tried playing audio using:

new Audio('/audioFile.mp3').play();

and

<audio id="audio" src="/audioFile.mp3" />
<script>document.getElementById('audio').play()</script>

Both work in my desktop browser, but not when casting a URL to the Chromecast.

Is there a supported method of playing audio on the Chromecast's browser?

Note: I'm not streaming a browser tab to the Chromecast, I'm directly casting the URL and it is rendering on the Chromecast device and not on any other devices

like image 293
Jane Panda Avatar asked Aug 04 '18 23:08

Jane Panda


1 Answers

I tested the new Audio(...).play() in my chromeCast and it worked. The audio was reproduced on my TV. This is the demo that worked:

window.onclick = function () {
  new Audio('https://ia802508.us.archive.org/5/items/testmp3testfile/mpthreetest.mp3').play();
};

Try it out: https://codepen.io/rafaelcastrocouto/pen/xJMPBv

Are you waiting for user input? Because Chrome will not start audio without it anymore, it will throw an error:

Uncaught (in promise) DOMException: play() failed because the user didn't interact with the document first. Read more https://developers.google.com/web/updates/2017/09/autoplay-policy-changes

like image 172
rafaelcastrocouto Avatar answered Nov 08 '22 08:11

rafaelcastrocouto