opus15 Posted August 20, 2009 Share Posted August 20, 2009 I'm not too fluent i php, so this may be ridiculously simple; My php project is a sort of database of my collection of mp3 files. They are all stored on my hard drive/external hard drive (C: and E:). The problem is how to load these files from the php application (running on apache server) - the project is going to stay on my pc, not to be uploaded to a different server - I'm guessing the problems with the server, finding c: ? I'll be using a javascript to play the files, or just embed that Quicktime thing. This does not work: $fil2 = "'file:///C:/RadioDB/2-4 Murder Unprompted.mp3'>" ; or this: <a href='http://C:\RadioDB/2-4 Murder Unprompted.mp3'> Any help appreciated! /jon Quote Link to comment https://forums.phpfreaks.com/topic/171146-load-local-mp3-files/ Share on other sites More sharing options...
ignace Posted August 20, 2009 Share Posted August 20, 2009 $genres = array("Blues","Classic Rock","Country","Dance","Disco","Funk","Grunge", "Hip-Hop","Jazz","Metal","New Age","Oldies","Other","Pop","R&B", "Rap","Reggae","Rock","Techno","Industrial","Alternative","Ska", "Death Metal","Pranks","Soundtrack","Euro-Techno","Ambient", "Trip-Hop","Vocal","Jazz+Funk","Fusion","Trance","Classical", "Instrumental","Acid","House","Game","Sound Clip","Gospel", "Noise","AlternRock","Bass","Soul","Punk","Space","Meditative", "Instrumental Pop","Instrumental Rock","Ethnic","Gothic", "Darkwave","Techno-Industrial","Electronic","Pop-Folk", "Eurodance","Dream","Southern Rock","Comedy","Cult","Gangsta", "Top 40","Christian Rap","Pop/Funk","Jungle","Native American", "Cabaret","New Wave","Psychadelic","Rave","Showtunes","Trailer", "Lo-Fi","Tribal","Acid Punk","Acid Jazz","Polka","Retro", "Musical","Rock & Roll","Hard Rock","Folk","Folk-Rock", "National Folk","Swing","Fast Fusion","Bebob","Latin","Revival", "Celtic","Bluegrass","Avantgarde","Gothic Rock","Progressive Rock", "Psychedelic Rock","Symphonic Rock","Slow Rock","Big Band", "Chorus","Easy Listening","Acoustic","Humour","Speech","Chanson", "Opera","Chamber Music","Sonata","Symphony","Booty Bass","Primus", "Porn Groove","Satire","Slow Jam","Club","Tango","Samba", "Folklore","Ballad","Power Ballad","Rhythmic Soul","Freestyle", "Duet","Punk Rock","Drum Solo","Acapella","Euro-House","Dance Hall"); $directories = array('path/to/directory1', 'path/to/directory2', ..); foreach ($directories as $directory) { foreach (glob("$directory/*.mp3") as $file) { $fh = fopen($file, 'r'); fseek($fh, -128, SEEK_END); $tag = fread($fh, 3); $data = array(); if ('TAG' === $tag) { $data['song'] = fread($fh, 30); $data['artist'] = fread($fh, 30); $data['album'] = fread($fh, 30); $data['year'] = fread($fh, 4); $data['comment'] = fread($fh, 30); $genre = fread($fh, 1); $data['genre'] = $genres[ord($genre)]; foreach ($data as $key => $value) { //echo $key, ' = ', $value, '<br>'; } } } } Quote Link to comment https://forums.phpfreaks.com/topic/171146-load-local-mp3-files/#findComment-902581 Share on other sites More sharing options...
opus15 Posted August 21, 2009 Author Share Posted August 21, 2009 Thanks ignace! Your code get met a lot closer to what I want, but not quite there... The $directory and $file works fine. The $fh gives me (when I echo it) a Resource id - is it this I'm going to open/play the file with? BTW, I've tried to implement the javascript for playing mp3 from this site: http://www.oreillynet.com/pub/a/oreilly/digitalmedia/2006/05/31/build-a-better-web-audio-player.html?page=2 (the "BatmoAudioPop.js") (This method works great - when I try it on a mp3 file stored in the same folder (easyphp/www/myfolder/) as my php application). Quote Link to comment https://forums.phpfreaks.com/topic/171146-load-local-mp3-files/#findComment-903304 Share on other sites More sharing options...
ignace Posted August 21, 2009 Share Posted August 21, 2009 Thanks ignace! Your code get met a lot closer to what I want, but not quite there... The $directory and $file works fine. The $fh gives me (when I echo it) a Resource id - is it this I'm going to open/play the file with? BTW, I've tried to implement the javascript for playing mp3 from this site: http://www.oreillynet.com/pub/a/oreilly/digitalmedia/2006/05/31/build-a-better-web-audio-player.html?page=2 (the "BatmoAudioPop.js") (This method works great - when I try it on a mp3 file stored in the same folder (easyphp/www/myfolder/) as my php application). Yeah you actually need recursion to go through the entire directory structure capture each and every mp3 file and read it's ID3 Quote Link to comment https://forums.phpfreaks.com/topic/171146-load-local-mp3-files/#findComment-903371 Share on other sites More sharing options...
ignace Posted August 21, 2009 Share Posted August 21, 2009 function scandir_r($directory) { $mp3files = array(); if (!is_dir($directory) || !is_readable($directory)) return $mp3files; $files = scandir($directory); foreach ($files as $file) { $fullpath = implode(DIRECTORY_SEPARATOR, array($directory, $file)); if (is_dir($fullpath)) { $mp3files += call_user_func(__FUNCTION__, $fullpath); } else if (preg_match('/\.mp3/', $file)) { $mp3files[] = $fullpath; } } return $mp3files; } $mp3files = array(); $directories = array('..'); foreach ($directories as $directory) { $mp3files += scandir_r($directory); } print_r($mp3files); Edit: I haven't tested this. I just wrote this out of the top of my head. If you have a serious amount of mp3 files your script may time-out and you may want to look at other algorithms available. Quote Link to comment https://forums.phpfreaks.com/topic/171146-load-local-mp3-files/#findComment-903378 Share on other sites More sharing options...
opus15 Posted August 23, 2009 Author Share Posted August 23, 2009 Thanks again, ignace! You're right about the time-out, but my biggest problem is that I don't quite understand what your last suggestion will do - I'm not that good at php... Will the $mp3files function return a filepath that I can use (with the javascript, for example) ? Jon Quote Link to comment https://forums.phpfreaks.com/topic/171146-load-local-mp3-files/#findComment-904502 Share on other sites More sharing options...
ignace Posted August 24, 2009 Share Posted August 24, 2009 Thanks again, ignace! You're right about the time-out, but my biggest problem is that I don't quite understand what your last suggestion will do - I'm not that good at php... Will the $mp3files function return a filepath that I can use (with the javascript, for example) ? Jon Yes it will return the full path to the file but you won't be able to use this with JavaScript. Quote Link to comment https://forums.phpfreaks.com/topic/171146-load-local-mp3-files/#findComment-904923 Share on other sites More sharing options...
ignace Posted August 24, 2009 Share Posted August 24, 2009 Here's the code that I was playing with function getPlayLength($mp3file) { $filesize = filesize($mp3file); return 0; } function getMusicGenre($genreId, $default = null) { static $genres = array( 'Blues', 'Classic Rock', 'Country', 'Dance', 'Disco', 'Funk', 'Grunge', 'Hip-Hop', 'Jazz', 'Metal', 'New Age', 'Oldies', 'Other', 'Pop', 'R&B', 'Rap', 'Reggae', 'Rock', 'Techno', 'Industrial', 'Alternative', 'Ska', 'Death Metal', 'Pranks', 'Soundtrack', 'Euro-Techno', 'Ambient', 'Trip-Hop', 'Vocal', 'Jazz+Funk', 'Fusion', 'Trance', 'Classical', 'Instrumental', 'Acid', 'House', 'Game', 'Sound Clip', 'Gospel', 'Noise', 'AlternRock', 'Bass', 'Soul', 'Punk', 'Space', 'Meditative', 'Instrumental Pop', 'Instrumental Rock', 'Ethnic', 'Gothic', 'Darkwave', 'Techno-Industrial', 'Electronic', 'Pop-Folk', 'Eurodance', 'Dream', 'Southern Rock', 'Comedy', 'Cult', 'Gangsta', 'Top 40', 'Christian Rap', 'Pop/Funk', 'Jungle', 'Native American', 'Cabaret', 'New Wave', 'Psychadelic', 'Rave', 'Showtunes', 'Trailer', 'Lo-Fi', 'Tribal', 'Acid Punk', 'Acid Jazz', 'Polka', 'Retro', 'Musical', 'Rock & Roll', 'Hard Rock', 'Folk', 'Folk-Rock', 'National Folk', 'Swing', 'Fast Fusion', 'Bebob', 'Latin', 'Revival', 'Celtic', 'Bluegrass', 'Avantgarde', 'Gothic Rock', 'Progressive Rock', 'Psychedelic Rock', 'Symphonic Rock', 'Slow Rock', 'Big Band', 'Chorus', 'Easy Listening', 'Acoustic', 'Humour', 'Speech', 'Chanson', 'Opera', 'Chamber Music', 'Sonata', 'Symphony', 'Booty Bass', 'Primus', 'Porn Groove', 'Satire', 'Slow Jam', 'Club', 'Tango', 'Samba', 'Folklore', 'Ballad', 'Power Ballad', 'Rhythmic Soul', 'Freestyle', 'Duet', 'Punk Rock', 'Drum Solo', 'Acapella', 'Euro-House', 'Dance Hall' ); return isset($genres[$genreId]) ? $genres[$genreId] : $default; } function getId3Tag($mp3file) { $data = array(); if ($fh = fopen($mp3file, 'r')) { fseek($fh, -128, SEEK_END); $tag = fread($fh, 3); if ('TAG' === $tag) { $data['song'] = trim(fread($fh, 30)); $data['artist'] = trim(fread($fh, 30)); $data['album'] = trim(fread($fh, 30)); $data['year'] = trim(fread($fh, 4)); $data['comment'] = trim(fread($fh, 30)); //$data['length'] = getPlayLength($mp3file); $data['genre'] = getMusicGenre(ord(fread($fh, 1))); } } return $data; } function getMusicFiles($directory) { $mp3files = array(); if (!is_dir($directory) || !is_readable($directory)) return $mp3files; $files = scandir($directory); foreach ($files as $file) { if ('.' === $file[0] || !preg_match('/\.(mp3|mp4|org|wav|wma)$/', $file)) continue; $fullpath = implode(DIRECTORY_SEPARATOR, array($directory, $file)); if (is_dir($fullpath)) { $mp3files = array_merge($mp3files, call_user_func(__FUNCTION__, $fullpath)); } else { $mp3files[] = $fullpath; } } return $mp3files; } Quote Link to comment https://forums.phpfreaks.com/topic/171146-load-local-mp3-files/#findComment-905225 Share on other sites More sharing options...
opus15 Posted September 3, 2009 Author Share Posted September 3, 2009 It works My only problem now is to find a way to play the mp3-files, either with a javascript or the Yahoo Media Player, or whatever works. Anyone any ideas? jon Quote Link to comment https://forums.phpfreaks.com/topic/171146-load-local-mp3-files/#findComment-911737 Share on other sites More sharing options...
ignace Posted September 3, 2009 Share Posted September 3, 2009 It works My only problem now is to find a way to play the mp3-files, either with a javascript or the Yahoo Media Player, or whatever works. Anyone any ideas? jon I would go for the Yahoo Media Player Quote Link to comment https://forums.phpfreaks.com/topic/171146-load-local-mp3-files/#findComment-911746 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.