Home > AS3.0, Flex, News, Projects > Import your iTunes Playlist into AS3 and Flex!

Import your iTunes Playlist into AS3 and Flex!

August 2nd, 2007

itunes_dg1.jpg

Ok, so first off let me say that the iTunes playlist XML structure looks like it was designed by someone who doesn’t even know XML.

Take a look at this entry for a single song:

<dict>
<key>Track ID</key><integer>64</integer>
<key>Name</key><string>Episode 14</string>
<key>Artist</key><string>Gareth Emery</string>
<key>Album</key><string>The Gareth Emery Podcast</string>
<key>Genre</key><string>Podcast</string>
<key>Kind</key><string>MPEG audio file</string>
<key>Size</key><integer>64803919</integer>
<key>Total Time</key><integer>2700068</integer>
<key>Year</key><integer>2006</integer>
<key>Date Modified</key><date>2006-09-21T15:14:43Z</date>
<key>Date Added</key><date>2006-09-21T15:14:43Z</date>
<key>Bit Rate</key><integer>192</integer>
<key>Sample Rate</key><integer>44100</integer>
<key>Play Count</key><integer>22</integer>
<key>Play Date</key><integer>3264149297</integer>
<key>Play Date UTC</key><date>2007-06-08T16:08:17Z</date>
<key>Release Date</key><date>2006-09-21T11:00:00Z</date>
<key>Sort Album</key><string>Gareth Emery Podcast</string>
<key>Persistent ID</key><string>FE2067001E4F2C00</string>
<key>Track Type</key><string>File</string>
<key>Podcast</key><true/>
<key>Location</key>
<string>The%20Gareth%20Emery%20Podcast/Episode%2014.mp3</string>
<key>File Folder Count</key><integer>4</integer>
<key>Library Folder Count</key><integer>1</integer>
</dict>

The way they have this structured makes it impossible to do any E4X based XML processing of the playlist and consequently prevents your from using it in Flex or any other MVC based framework that allows you to easily hook an XML data source into components on the view layer.

So, as if you couldn’t guess, I wrote a class so that you can. You can use it to load in an iTunes XML playlist and turn it into something use-able that adheres to XML best practice where the structure is concerned.

Heres a sample of what it outputs:

<song>
  <Track_ID>511</Track_ID>
  <Name>Episode 34</Name>
  <Artist>Gareth Emery</Artist>
  <Album>The Gareth Emery Podcast</Album>
  <Genre>Podcast</Genre>
  <Kind>MPEG audio file</Kind>
  <Size>64804016</Size>
  <Total_Time>2700068</Total_Time>
  <Year>2007</Year>
  <Date_Modified>2007-07-24T16:29:57Z</Date_Modified>
  <Date_Added>2007-07-24T16:29:57Z</Date_Added>
  <Bit_Rate>192</Bit_Rate>
  <Sample_Rate>44100</Sample_Rate>
  <Release_Date>2007-07-19T22:00:00Z</Release_Date>
  <Sort_Album>Gareth Emery Podcast</Sort_Album>
  <Sort_Name>Gareth Emery Podcast - Episode 34</Sort_Name>
  <Persistent_ID>DFA5DE7CE96B0316</Persistent_ID>
  <Track_Type>File</Track_Type>
  <Podcast/>
  <Unplayed/>
  <Location>The%20Gareth%20Emery%20Podcast/Episode%2034.mp3</Location>
  <File_Folder_Count>4</File_Folder_Count>
  <Library_Folder_Count>1</Library_Folder_Count>
</song>

Much better, huh? Heres an example of how you can use it in your Flex/AS3.0 project.

var libraryPath:URLRequest = new URLRequest("testlibrary.xml");
 
	iTunesLibrary = new itunesXMLParser();
	iTunesLibrary.load(libraryPath);
 
	iTunesLibrary.addEventListener(Event.COMPLETE, loadHandler);
	iTunesLibrary.addEventListener(ProgressEvent.PROGRESS, progressHandler);
 
public function loadHandler(event:Event):void{
	trace("Library is done loading, lets do something cool with it");
 
	//trace(iTunesLibrary.playlistXML);
 
	trace("songs: "  + iTunesLibrary.playlistXML.song);
 
	var songList:XMLList = iTunesLibrary.playlistXML.song;
 
	//trace(songList);
 
	dg_mx.dataProvider = songList;
}
 
public function progressHandler(event:ProgressEvent):void{
	trace("progressHandler loaded:" 
        + event.bytesLoaded + " total: " + event.bytesTotal)</code>
}

Note that it listens on all the same events as the normal XML class. In the loadHandler function above theres an example of how to hook this into a datagrid component in flex.

Thats it. Right now it only grabs songs, I’ll be adding support for multiple playlists later.

Download the class file here

Download the example flex project here

ian AS3.0, Flex, News, Projects

  1. No comments yet.
  1. No trackbacks yet.
You must be logged in to post a comment.