V
V
Vladimir Fedorov2015-09-29 00:11:29
JavaScript
Vladimir Fedorov, 2015-09-29 00:11:29

Random audio playback?

How can I implement random sound playback?
At the moment I have it set up like this:

if (sound == 'on') {
        $('#bet2-sound')[0].play();
      }

I need to select any of the three sounds
bet1 or bet2 or bet3
If the sound is on and a certain action is performed, then the bet2 sound is played. I
defined it like this
In the Header
<audio id="bet2-sound" src="/sounds/Stavka-2.mp3" preload="auto"></audio>

Answer the question

In order to leave comments, you need to log in

3 answer(s)
G
Green Elephant, 2015-09-29
@GreenElephantt

Initially, a sound is randomly selected,
then in the event handler for which Sound2 is played, there is a check to see
if Sound2 is turned on, i.e.

{
        $('#bet2-sound')[0].play();
}

S
Stalker_RED, 2015-09-29
@Stalker_RED

We generate a random number in the desired range

function getRandomIntInRange(min, max) {
    return Math.floor(Math.random() * (max + 1 - min) + min);
}

Playing the corresponding recording
function playRandom() {
    var rnd = getRandomIntInRange(1, 3)
    document.getElementById('sound-' + rnd).play();
}

Demo

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question