5

I'm trying to launch automatically a webradio on my html's file. Currently using autoplay fails, I assume the radio miss the time to load some data hence the autoplay just block. I have created a setTimeout function to handle that. Now the function refuses to launch on Firefox and Opera. The error log of Firefox is far most informative, here reproduced:

NotAllowedError: The play method is not allowed by the user agent or the platform in the current context, possibly because the user denied permission

here my html's snippet:

<body>
  <audio controls src="http://5.63.151.52:7136/stream?icy=http" autoplay id="myAudio">
                Your browser does not support the
                <code>audio</code> element.
        </audio>

  <button onclick={goPlay()}>
            Play
        </button>

  <script>
    // function goPlay(){
    //     console.log("in goplay")
    setTimeout(() => {
        var x = document.getElementById("myAudio");
        // function playAudio() {
        x.play();
      },
      1000
    )
    // }
  </script>
</body>

How handle this situation?

Any hint would be great, thanks

Jonas
  • 121,568
  • 97
  • 310
  • 388
Webwoman
  • 10,196
  • 12
  • 43
  • 87

2 Answers2

3

The Firefox error is telling you exactly what's happening. The autoplay isn't working because the user (or more specifically, the browser - see here) is blocking it.

Any playback that happens before the user has interacted with a page via a mouse click, printable key press, or touch event, is deemed to be autoplay and will be blocked if it is potentially audible.

Matt
  • 2,063
  • 1
  • 14
  • 35
1

In my case, when I tried to execute document.getElementsByTagName("video")[0].play() in the browser console, above the error message you mentioned, there was a warning Autoplay is only allowed when approved by the user, the site is activated by the user, or media is muted.. I was able to execute the code by muting the video first or selecting "Allow Audio and Video" for the website permissions in Firefox.

baptx
  • 3,428
  • 6
  • 33
  • 42
  • Yes currently some browsers restrict the autoplay's function to preserve the serenity of their user, it can lead to error effectively. – Webwoman Apr 02 '20 at 17:06