Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Apr 2010
    Posts
    417
    Thanks
    4
    Thanked 1 Time in 1 Post

    how to randomize mp3 playlist, instead of ordered playlist

    I have a set of mp3 music files that i wish to have played on my site, but I would like them to play in a random order.

    How could I do this ?

  • #2
    Regular Coder
    Join Date
    Jan 2010
    Posts
    137
    Thanks
    3
    Thanked 10 Times in 10 Posts
    What are you using to play them right now?
    Always happy to help with any design/code issues.
    Jenius Design (beta, but almost finished)

  • #3
    New Coder
    Join Date
    Jan 2011
    Posts
    11
    Thanks
    0
    Thanked 1 Time in 1 Post
    Why do you want them to play in random order?

  • #4
    New Coder
    Join Date
    Dec 2010
    Posts
    33
    Thanks
    0
    Thanked 1 Time in 1 Post
    Follow these steps to play MP3 files in a random order on your web page:
    1. 1. Create a playlist file called playlist.m3u containing all of the songs you want, in any order.
    2. 2. Upload playlist.m3u to your website. When you do, make a note of the file system path to playlist.m3u on the server. If your FTP program shows that the folder is /home/sites/yourname/www.example.com/examples/, then the path to playlist.m3u is /home/sites/yourname/www.example.com/examples/playlist.m3u. Note: this is NOT a URL. It is the place on the server's hard drive where the file lives. If you don't understand this, reread it until you do!
    3. 3. Create the file randomsongs.php, containing the following. There must be ABSOLUTELY NO BLANK LINES OR WHITE SPACES AT THE BEGINNING! Otherwise it will be too late for the script to output a playlist instead of an HTML page when that is appropriate.

      Code:
      <?php
       $playlist = "/home/sites/myname/www.example.com/examples/playlist.m3u";
       if ($_SERVER['PATH_INFO'] == "/playlist.m3u") {
        # This a request for the actual playlist.
        playlist();
       } else {
        # Fall through to end of script and display
        # the player HTML.
       }
       function playlist() {
        header("Content-type: audio/mpeg");
      
        # Needed for PHP versions OLDER than 4.2.0 only.
        # If your host still has PHP older than 4.2.0, shame on them.
        # Find a better web host.
        srand(make_seed());
      
        # Fetch our list of songs from a file.
        $songs = file($playlist);
        shuffle($songs);
        # Now output the URLs in random order.
        foreach ($songs as $song) {
         # Remove newline and any other leading and trailing
         # whitespace from URL of song.
         $song = trim($song);
         echo "$song\n";
        }
        # Now exit before any HTML is produced.
        exit(0);
       }
       # Needed only for very old versions of PHP,
       # see srand call earlier.
       function make_seed()
       {
        list($usec, $sec) = explode(' ', microtime());
        return (float) $sec + ((float) $usec * 100000);
       }
      ?>
      
      <html>
      <head>
      <title>MP3s Playing in Random Order</title>
      </head>
      <body>
      <h1 align="center">MP3s Playing in Random Order</h1>
      <embed src="/examples/randomsongs.php/playlist.m3u"
       width="0"
       height="0"
       autostart="true"
       type="audio/mpeg"
       loop="true"/>
      </body>
      </html>
    4. 4. At the top of randomsongs.php change the setting of the $playlist variable to match the file system path of your playlist file... which you wrote down in step two.
    5. 5. randomsongs.php assumes you will be placing your PHP page in a folder on your website called /examples. If that's not right, change the src attribute of the embed element. DO NOT remove /playlist.m3u from the end of the URL. If you want to know why it's there, read the technical notes at the end of this entry.
    6. 6. Upload randomsongs.php to your web server.
    7. 7. Upload your .mp3 files if you have not already done so.
    8. 8. Access randomsongs.php with your web browser. In our example, the URL would be http://www.example.com/examples/randomsongs.php, but of course your URL will be different. Your songs will begin to play in a random order.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •