PHP MP3 playlist


/ Published in: PHP
Save to your folder(s)

I found this script out in space and think it's rad. Click on the URL to see this authors profile. The easiest way to use this script is to place it in the same directory as your mp3's then make a request to the php. The php will return an XML with the song Path, Title, and Author which is great for Flash.


Copy this code and paste it in your HTML
  1. <?php
  2.  
  3. $dir = 'directory_of_mp3_files';
  4. $file_type = 'mp3';
  5.  
  6. $play_list = '<?xml version=\'1.0\' encoding=\'UTF-8\' standalone=\'yes\'?>';
  7. $play_list .= "<playlist>";
  8.  
  9. // Open directory, read contents and add to file_list if correct file_type
  10. if (is_dir($dir)) {
  11. if ($dh = opendir($dir)) {
  12. while (($file = readdir($dh)) !== false) {
  13. if ($file != '.' && $file != '..') {
  14. $name_array = explode('.', $file);
  15.  
  16. // if file has .mp3 extension
  17. if ($name_array[1] == $file_type) {
  18. $play_list .= '<song>';
  19. $file = "$dir/$file";
  20. $play_list .= '<file>' . $file . '</file>';
  21. $mp3 = fopen($file, "r");
  22. fseek($mp3, -128, SEEK_END);
  23. $tag = fread($mp3, 3);
  24.  
  25. // if id3 tag is found...
  26. if ($tag == "TAG") {
  27. $play_list .= '<title>' . trim(fread($mp3, 30)) . '</title>';
  28. $play_list .= '<artist>' . trim(fread($mp3, 30)) .'</artist>';
  29.  
  30. // if no id3 tag...
  31. } else {
  32. $play_list .= '<title>unknown title</title>';
  33. $play_list .= '<artist>unknown artist</artist>';
  34. }
  35.  
  36. // close file
  37. fclose($mp3);
  38. $play_list .= '</song>';
  39. }
  40. }
  41. }
  42.  
  43. // close directory
  44. closedir($dh);
  45. $play_list .= '</playlist>';
  46.  
  47. // echo the xml file
  48. echo "$play_list";
  49. }
  50. }
  51.  
  52. ?>

URL: http://www.gotoandlearnforum.com/memberlist.php?mode=viewprofile&u=954

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.