-1

I want to create an easy shuffle audioplayer based on HTML 5 and PHP. I have 4 different sound files (length of 1 second each file).

My aim is to change the sound file randomly, but currently I need to reload the page before the next file plays.

    <?php
$audioFile1 = '<source src="path/SoundFile1.wav" type="audio/mpeg">';
$audioFile2 = '<source src="path/SoundFile2.wav" type="audio/mpeg">';
$audioFile3 = '<source src="path/SoundFile3.wav" type="audio/mpeg">';
$audioFile4 = '<source src="path/SoundFile4.wav" type="audio/mpeg">';
$audioFiles = array( $audioFile1, $audioFile2, $audioFile3, $audioFile4 );
shuffle( $audioFiles );
?>

<audio controls class="iru-tiny-player" data-title="Shuffle">
    <?php print $audioFiles[0]; ?>
</audio>

Is there a way to shuffle the next file after ending the current file ?

Filip
  • 77
  • 2
  • 11

2 Answers2

0

I would print the shuffled paths into a JS array, and then listen to the ended event of the <audio> element, then change the src attribute of the source to the next track:

const shuffledPaths = [
  'https://freewavesamples.com/files/Alesis-Sanctuary-QCard-Tines-Aahs-C4.wav',
  'https://freewavesamples.com/files/Roland-JV-2080-Pick-Bass-C2.wav',
  'https://freewavesamples.com/files/Bass-Drum-2.wav'
];

// You should probably use more specific selectors, this is for the sample
const player = document.querySelector('audio');
const source = document.querySelector('source');

let currentIndex = -1;

function goNext() {
  // Grab the next path and put it in the source's src attribute
  currentIndex = (currentIndex + 1) % shuffledPaths.length;
  source.setAttribute('src', shuffledPaths[currentIndex]);

  // Load the corresponding track and play it
  player.load();
  player.play();
}

// Play the following track when the current one ended
player.addEventListener('ended', goNext);

// Play the first track
goNext();
<audio controls class="iru-tiny-player" data-title="Shuffle">
  <source type="audio/mpeg">
</audio>

Note that I made it loop but it's easy to play around with currentIndex to make it stop at the end if it's preferred.

Jeto
  • 14,596
  • 2
  • 32
  • 46
0

You can change you code little and work with js.

<?php
    $audioFile1 = 'path/SoundFile1.wav';
    $audioFile2 = 'path/SoundFile2.wav';
    $audioFile3 = 'path/SoundFile3.wav';
    $audioFile4 = 'path/SoundFile4.wav';
    $audioFiles = array($audioFile1, $audioFile2, $audioFile3, $audioFile4);
    shuffle($audioFiles);
?>

<audio id="audio" controls class="iru-tiny-player" data-title="Shuffle" src="<?php print $audioFiles[0] ?>" type="audio/mpeg"></audio>

<script type="text/javascript">
    document.getElementById('audio').addEventListener("ended",function() {
        var fileArr = ['<?= $audioFile2 ?>', '<?= $audioFile2 ?>', '<?= $audioFile4 ?>'];
        var min=0;
        var max= (fileArr.length -1);
        var random = Math.floor(Math.random() * (+max - +min) + +min);
        this.src = fileArr[random];
        this.play();
    });
</script>
Rohit Rasela
  • 425
  • 3
  • 6