Jump to content

Shuffle MP3


Fijiannn

Recommended Posts

So in my code below I have a script where it shuffles through .mp3 in a folder. How can I make the line below know which song the shuffle() has picked? 

 $ThisFileInfo = $getID3->analyze($Songs[0]);
 

 

<?php
    require_once('/home/manveers94/public_html/getID3-1.9.7/getid3/getid3.php');
    $getID3 = new getID3;
    
    function fooFucker($url) {
        $a = join('/', array_map('urlencode', explode('/', $url)));
        return str_replace('+', '%20', $a);
    }
    
    $SongsFolder = "songs";
    
    header("Content-Type: audio/x-mpegurl");
    
    $body = '';
    
    $Songs = glob($SongsFolder."/*.mp3");
    shuffle($Songs);
    $first = 0;
    
    foreach($Songs as $Song)
    {
        $SongPath = pathinfo($Song);
        if ($first != 0) {
            $body .= "\n";
        }
        $first = 1;
        $body .= "http://".$_SERVER['HTTP_HOST'].fooFucker(dirname($_SERVER['PHP_SELF']))."/".fooFucker($SongsFolder)."/".fooFucker($SongPath['basename']);
    }
    
    set_time_limit(30);
    
    $ThisFileInfo = $getID3->analyze($Songs[0]);
    getid3_lib::CopyTagsToComments($ThisFileInfo);
        
    $body .= 'File name: '.$ThisFileInfo['filenamepath'].'<br>';
    $body .= 'Artist: '.(!empty($ThisFileInfo['comments_html']['artist']) ? implode('<BR>', $ThisFileInfo['comments_html']['artist']) : ' ').'<br>';
    $body .= 'Title: '.(!empty($ThisFileInfo['comments_html']['title']) ? implode('<BR>', $ThisFileInfo['comments_html']['title'])  : ' ').'<br>';
    $body .= 'Bitrate: '.(!empty($ThisFileInfo['audio']['bitrate']) ? round($ThisFileInfo['audio']['bitrate'] / 1000).' kbps'   : ' ').'<br>';
    $body .= 'Play time: '.(!empty($ThisFileInfo['playtime_string']) ? $ThisFileInfo['playtime_string']                          : ' ').'<br>';
    
    header('Content-Length: ' . strlen($body));
    echo $body;
?>
Link to comment
Share on other sites


$songs = array('song a','song b','song c'); # the songs in order a, b, c
shuffle($songs); # mix up the songs! PROBABLY not in order any more
echo $songs[0]; # get the first song from the $songs array, which is PROBABLY not 'song a' since the songs were PROBABLY mixed up.

## PROBABLY IS A VERY IMPORTANT WORD HERE
Link to comment
Share on other sites

Hey,

 

You need to put that line inside the foreach you have so it becomes

 

$ThisFileInfo = $getID3->analyze($Song);
and remember to put that into the foreach.

 

After you shuffle and look at $songs[0], chances are that's not actually the first one anymore.

 

The best way to test what's actually happening is do a print_r on $songs before you shuffle, then after and put an exit in. That way, you can see if 0 is working in the way of looking at the first one or if it goes off somewhere else.

Link to comment
Share on other sites

 

$songs = array('song a','song b','song c'); # the songs in order a, b, c
shuffle($songs);       # mix up the songs! PROBABLY not in order any more
echo $songs[0];        # get the first song from the $songs array, which is PROBABLY not 'song a' since the songs were PROBABLY mixed up.

## PROBABLY IS A VERY IMPORTANT WORD HERE

Why would I want to make an array of every song? I have over a 400+ songs in the folder with mp3 in it.

Link to comment
Share on other sites

Why would I want to make an array of every song? I have over a 400+ songs in the folder with mp3 in it.

 

Well, if you don't want to make an array of every song.... you better delete  $Songs = glob($SongsFolder."/*.mp3");    because that's exactly what it's doing

Link to comment
Share on other sites

Okay well, how can I do it using that?

 

You need an array to store all of your songs and shuffle them using the shuffle((array) $array) function, You are not making an array of every song, you are storing every song in 1 array.

Edited by GetFreaky
Link to comment
Share on other sites

You need an array to store all of your songs and shuffle them using the shuffle((array) $array) function, You are not making an array of every song, you are storing every song in 1 array.

 

Right!  We're not making an array for every song, we're making an array of every song (that matched the glob pattern).

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.