N
N
Nekio2021-07-21 14:33:18
JavaScript
Nekio, 2021-07-21 14:33:18

Is it possible in JS to automatically play an audio file on page load?

It is necessary that the audio message be displayed when entering the page.

Now throws an error:
Uncaught (in promise) DOMException: The play method is not allowed by the user agent or the platform in the current context, possibly because the user denied permission.

Or is there a way to ask the user to remove the ban on playing audio files in the browser?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
S
Sergey delphinpro, 2021-07-21
@QuayS1de

Currently not possible. What, in fact, you are informed.
Playback must be initiated by the user. Usually it's a mouse click.
Dirty methods used to work, like reacting to a mousemove or onscroll event. However, at the moment, in all browsers, this shop, thank God, was closed.
The only option left is to force the user to listen to your music, although this is not autoplay in the literal sense: a transparent div on top of the entire site, by clicking on which, it is deleted and playback starts. The method is also dirty, users may not like it and they will stupidly leave you. Especially if there is no way to stop the music.
Also, if playback has already been started by the user, then when switching between pages of your site, autoplay will already work within the current session (before leaving the site).

A
Armenian Radio, 2021-07-21
@gbg

Not allowed. And this is good.

A
Alexander, 2021-07-21
@Aleksandr-JS-Developer

You can display some kind of banner, they say, so and so, please turn on the audio and click here on the play

E
ExactlyDude, 2021-07-21
@ExactlyDude

<audio src="/music.mp3" autoplay></audio>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question