Auto play(audio) with Javascript

Asked

Viewed 289 times

-1

There is some method of auto play with audio, I tried several ways but none worked. Usually works on a pc and the test pc does not work.

I have tested with iframe, video, audio, direct on Js and Youtube Api.

<audio id="musica" autoplay="true">
    <source src="/home/musicas/teste.ogg" type="audio/ogg">
    <source src="/home/musicas/teste.mp3" type="audio/mp3">
</audio>

2 answers

2

As I do not have enough points to comment, I put here:

Chrome blocks autoplay with sound when the user has not performed any interaction with the page by security measures. As specified here, in New behaviors.

As you may have noticed, web browsers are Moving Towards stricter autoplay policies in order to improve the user Experience, minimize incentives to install ad Blockers, and reduce data Consumption on Expensive and/or constrained Networks. These changes are intended to Give Greater control of playback to users and to Benefit Publishers with legitimate use cases.

0

Add the Javascript call to the body of your html, so once the page is rendered the JS will run:

function play() {
  document.getElementById("musica").play();
}

play();
  • Unfortunately it does not work with this method.

  • True, this Chrome update really blocked it. I think you’ll have to play when the user does some interaction, like a click, or something like

Browser other questions tagged

You are not signed in. Login or sign up in order to post.