Audio aleatorio na pagina

Asked

Viewed 210 times

1

I wish that whenever someone walks into a page some random mail is reproduced that is 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 I enter the page it carries all Audios at once. Someone can help me ?

when I look at the source code appears all the audios that are in the folder.

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

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

3 answers

2

To get all the songs you can use the glob, thus:

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

This will list all .mp3 and .wav that are in the folder data, 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 one randomly it would be preferable to use a CSPRNG, so could use the 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 don’t 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 the array_rand:

<?php

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

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

?>

<audio loop='loop' autoplay='autoplay'>
   <source src='<?= $musica ?>'>
</audio>
  • Thanks, it worked. however I used a shuffle($files); before for, and a break; after audio echo

0

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

 echo "<audio loop='loop' autoplay='autoplay'>
                 <source src='$dir_path$files[$numberAleatorio]'>
                  </audio> ";
  • I’m already counting the number of files for($i = 0; $i < Count($files); $i++)

  • 1

    So just do: Rand(1, Count($files));

  • Something is wrong, when I look at the source code the two Audios,<audio loop='loop' autoplay='autoplay'> <source src='data/defacee.mp3'> </audio> <audio loop='autoplay='autoplay'> <source src='data/sound.mp3'> </audio>

0


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

   <?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;



                }
            }
        }
    }
    ?>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.