Random Audio on page

1

I always wanted someone to enter a page to play some random random ale in a folder.

I tried to use this

<?php

$dir_path = "data/";
$extensions_array = array('mp3');

if(is_dir($dir_path))
{
    $files = scandir($dir_path);

    for($i = 0; $i < count($files); $i++)
    {
        if($files[$i] !='.' && $files[$i] !='..')
        {

            $file = pathinfo($files[$i]);
            $extension = $file['extension'];


            if(in_array($extension, $extensions_array))
            {

            echo "<audio loop='loop' autoplay='autoplay'>
                 <source src='$dir_path$files[$i]'>
                  </audio> ";



            }
        }
    }
}
?>

But on the page it loads all audios at once. Can someone help me?

When I look at the source code, all the audio files that are in the folder appear.

<audio loop='loop' autoplay='autoplay'> <source src='data/defacee.mp3'></audio>

 <audio loop='loop' autoplay='autoplay'> <source src='data/sound.mp3'> </audio>
    
asked by anonymous 17.05.2017 / 18:22

3 answers

0

I put shuffle($files); p to choose a random file before the for and a Break; after the echo of the audio.

   <?php

    $dir_path = "data/";
    $extensions_array = array('mp3');

    if(is_dir($dir_path))
    {
        $files = scandir($dir_path);


        shuffle($files);
        for($i = 0; $i < count($files); $i++)
        {
            if($files[$i] !='.' && $files[$i] !='..')
            {

                $file = pathinfo($files[$i]);
                $extension = $file['extension'];


                if(in_array($extension, $extensions_array))
                {

                echo "<audio loop='loop' autoplay='autoplay'>
                     <source src='$dir_path$files[$i]'>
                      </audio> ";

                      Break;



                }
            }
        }
    }
    ?>
    
18.05.2017 / 19:37
2

To get all the songs you can use glob , like this:

$musicas = glob('data/*.{mp3,wav}', GLOB_BRACE);

This will list all .mp3 and .wav that are in data folder, for example:

array (
  0 => 'data/07. Alphaverb & Intractable One - Turbulence (FRGMNT_7).mp3',
  1 => 'data/Frontliner - What You Come For (Radio Edit).mp3',
  2 => 'data/Frontliner - Around The World (Extended Mix).wav',
)

* If you use another format, add it to the filter of glob . *

So to choose a randomly it would be preferable to use a CSPRNG, then you could use random_int() , requires PHP 7 + :

<?php

$musicas = glob('data/*.{mp3,wav}', GLOB_BRACE);   

$musica = $musicas[random_int(0, count($musicas) - 1)];

?>

<audio loop='loop' autoplay='autoplay'>
    <source src='<?= $musica ?>'>
</audio>

If you do not want to use a CSPRNG you can choose a Mersenne-twister (Spotfy uses it, for example) then use:

<?php

$musicas = glob('data/*.{mp3,wav}', GLOB_BRACE);

$musica = $musicas[mt_rand(0, count($musicas) - 1)];

?>

<audio loop='loop' autoplay='autoplay'>
   <source src='<?= $musica ?>'>
</audio>

If not, you can use array_rand :

<?php

$musicas = glob('data/*.{mp3,wav}', GLOB_BRACE);

$musica = $musicas[array_rand($musicas)];

?>

<audio loop='loop' autoplay='autoplay'>
   <source src='<?= $musica ?>'>
</audio>
    
17.05.2017 / 19:54
0

Use Math.random (); to generate a random number in the range of 1 up to the amount of audio files that your directory contains, after that just pass that value.

 echo "<audio loop='loop' autoplay='autoplay'>
                 <source src='$dir_path$files[$numberAleatorio]'>
                  </audio> ";
    
17.05.2017 / 18:42