Next entry >>
Category: Uncategorized

This code is meant for pls files but could easily be adapted for m3u. no benefit I can see in doing that though compared to pls.

-----

So for a little personal project I'm doing in flash I'm building an mp3 player.

Playing mp3's with flash is pretty easy and and making some buttons with play pause etc is also not really an issue. But loading a playlist file... well now we have a problem.

Most people seem to want to just creat an xml file becuase flash does that easily. But that seems like a little lazyness at the start and alot of tedious work down the road creating xml files all the time. I'd rather not have all that extra work later on and I'd rather have native support of .pls files for playing music.

I googled and couldn't find anyone who got this running except some guy, ming chan with m3u years ago, but the source is since gone and disapeared. So for anyone who's also stuck, please feel free to use my actionscript code, but if you improve it (It's messy I know...) please post in the comments.

Anyways here's the source code for parsing a .pls file in actionscript2.

//used to clean up the playlist -meant to work exact same as // php str_replace. I hate that flash doesn't have it. function str_replace(search, replace, string) { var array = string.split(search); return array.join(replace); } //////lets open the .pls file and load it into memoriez! var scope:MovieClip = this; var arr:Array = new Array(); var myLoadData:LoadVars = new LoadVars(); myLoadData.load("test.pls"); myLoadData.onData = function(success) { /* .pls files use groups of 3. first file location, then track name, then the length of character sin the track name. not really any need for the 3rd one. but who knows. */ arr = success.split("\n"); ////////////// mp3_playlist_file_location_array = new Array(); mp3_playlist_file_title_array = new Array(); mp3_playlist_item_counter = 0; current_track_number = 1; //don't start at zero for (var a = 1; a<arr.length; a++) { mp3_playlist_item_counter++;//first is 1 if (mp3_playlist_item_counter == 1) { //file location cleaned1 = arr[a]; cleaned1 = cleaned1.str_replace('File'+ current_track_number + '=', 'C:'); cleaned1 = str_replace('File' + current_track_number+'=', 'C:', cleaned1); mp3_playlist_file_location_array.push(cleaned1); } else if (mp3_playlist_item_counter == 2) { //title cleaned2 = arr[a]; cleaned2 = str_replace('Title' + current_track_number+'=', '', cleaned2); mp3_playlist_file_title_array.push(cleaned2); } else if (mp3_playlist_item_counter == 3) { //is this even used? length = number of chars in title. mp3_playlist_item_counter = 0; current_track_number++; } } /* insert your own function here that uses these 2 arrays. only here doing so outside of the function would mean it would be called BEFORE we parse the data */ //my_function_to_use_this_data(); };

 

It's pretty raw and brute force, but it works just fine. You have 2 string arrays at the end, one with the file location and one with the id3 name. unfortunetly track time isn't in a .pls file (it's also not in an m3u either) so you have to read that on your own with another function. ...maybe I'll post that after I write it.

 
| Newest comments last| Back


Add a Comment

Comments must first be APPROVED before they are publicly viewable.
*Name:
Email:
(Hidden)
url:
*Comments:
  Remember my details
*Verify:
 
Please type the verification string for security purposes.
(* Required).
Line breaks and paragraphs are automatically converted
All HTML tags will be removed.
Comment Preview
says:

Posted on Wednesday February 08, 2012 @ 03:24am


Page 1 of 1