1

On my website I have an embedded audio that automatically starts and runs in the background (I have the audio embedded, because a normal can't be autoplayed in chrome). Now I am trying to find a way to make that audio start at a random point in the clip.

<body onload="audiofunction()">
        <embed id="audio" src="berlin.mp3" type="audio/mpeg" autostart="true" loop="true">

        <script>
            function audiofunction(){
                var audio = document.getElementById("audio");
                alert(audio);
                var max = audio.duration + 1;
                audio.currentTime = Math.floor(Math.random() * max);
            }
        </script>
</body>

That is how my Code looks like. I know that it works on normal audio, but with the embedded audio nothing happens.

Maybe you could help me.

luckylou
  • 11
  • 1
  • 1
    In a specific version of Chrome? Both your example and with the `audio` tag works in my version of Chrome. Maybe the problem is somewhere else? Try create a [MCVE](https://stackoverflow.com/help/minimal-reproducible-example) – David Fontes Jul 19 '19 at 15:04
  • But does it also choose a random position in the clip, when you try it? – luckylou Jul 19 '19 at 15:34
  • I did try but it does not work because the `embed` element does not have a `currentTime` property nor a `duration` one. – David Fontes Jul 19 '19 at 15:55
  • Okay i did not know that. I guess I have to find a way to make the normal audio work. Thank you – luckylou Jul 19 '19 at 16:26
  • Check out this [fiddle](https://jsfiddle.net/Lx6pf13g/) and let me know if it works for you. As you mentioned though, this may not work for every user. For more information, check this [answer](https://stackoverflow.com/a/50490453/11755228) from a different question. – David Fontes Jul 19 '19 at 16:28
  • Where is mp3 located? Same site as the page or on a site that you can't edit? It's impossible for the latter but might be possible for the former – zer00ne Jul 19 '19 at 17:09
  • The mp3 is my own, so it is located on the same site. – luckylou Jul 28 '19 at 10:33

0 Answers0