The Dead Man
The Dead Man

Reputation: 5566

How to add random src on audio

I have simple audio as background like this

<audio id="audioplayer" playsinline="" controls="" muted="" loop="" autoplay="" hidden="">
            <source src="audio/muzyka_meed_loop.mp3" type="audio/mpeg">
            Your browser does not support the audio element.
  </audio> 

I want each time page is reloaded it should generate a new random number like this.

<audio id="audioplayer" playsinline="" controls="" muted="" loop="" autoplay="" hidden="">
            <source src="audio/muzyka_meed_loop.mp3?rand=0.8514959603294041" type="audio/mpeg">
            Your browser does not support the audio element.
        </audio>

I have tried to add random math like this

<audio id="audioplayer" playsinline controls muted loop autoplay hidden>
            <source src="audio/muzyka_meed_loop.mp3?rand= + Math.random()"  type="audio/mpeg">
            Your browser does not support the audio element.
        </audio>

This is not working , what do I need to change to get this working?

Upvotes: 2

Views: 149

Answers (1)

Rain336
Rain336

Reputation: 1498

The src attribute in HTML does not process JavaScript. You need to find the source tag using for example JQuery to change the src attribute that way.

$('#audioplayer source').each(function() {
    $(this).attr('src', 'audio/muzyka_meed_loop.mp3?rand=' + Math.random());
});

Upvotes: 2

Related Questions