Tải bản đầy đủ (.pdf) (12 trang)

Extracting ID3 Data From an MP3 File

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (38.65 KB, 12 trang )


< Day Day Up >

Extracting ID3 Data From an MP3 File
Do you think an MP3 file holds nothing but code for playing back an audio file? Think
again. Most people are unaware that hidden in most MP3 files are ID3 tags. These tags,
known as metadata, contain information such as the following about the MP3 file:

Name of the song

Artist

Genre

Year the song was recorded

Album from which the song came

Lyrics

Comments
Most modern MP3 players can read, display, and react to this included data when playing
a file. Thankfully, ActionScript also provides access to this information when using MP3
files in your projects. This fact allows you to create sophisticated audio applications.
NOTE
Not all MP3s contain ID3 tags.

When an MP3 is loaded into or attached to a Sound object, that file's ID3 tag information
can be read by accessing the Sound object's ID3 property. For example, the ID3 tag
representing a song name is TIT2. To extract this data from the MP3 file loaded into a
Sound object named music, you would use the following syntax:



var songName_txt.text = music.ID3.TIT2;


Other common ID3 tags include TALB (album), TPE1 (artist), TYER (year of
recording), TRCK (track), and COMM (comment).
TIP
For more information about ID3 tags, consult the ActionScript dictionary or visit
www.id3.org
. Many ID3 tag editors are available to help you add your own ID3 tags to
MP3 files. A search on www.google.com will return a number of possibilities.

In the following exercise, you'll create an MP3 player that loads MP3 files based on data
in an XML file. The MP3 files contain ID3 tag data that will be extracted and displayed
on the user interface.
1. Open mp3Player1.fla in the Lesson18/Assets folder.
This project contains five layers named Background, Text Fields, Buttons,
Components, and Actions. Our project's static graphics are on the Background
layer. There are five text fields on the Text Fields layer: song_txt, artist_txt,
year_txt, URL_txt, and comments_txt. These fields will eventually be used to
display extracted ID3 tag data. The Buttons layer contains two button instances
named play_btn and stop_btn. Obviously, these will be used as playback controls
for the currently selected MP3 file. The Components layer contains a List
component instance named playlist_lb, which will be used to display a list of
available MP3 files loaded in from an external XML file (we'll discuss this in a
moment). The Actions layer will contain all the script for this project.
This project will work by loading a list of MP3 files from an external XML file
and displaying the list in the playlist_lb instance. When the user selects an MP3
file from the list and clicks the play_btn instance, that MP3 file not only plays, but
its ID3 tag data is extracted and displayed in the text fields on the Text Fields

layer.

Before we begin scripting, let's review the external files that this project will use.
2. Using your operating system's directory-exploring application, navigate to the
Lesson18/Assets directory and locate the files Agent00.mp3, HardCope.mp3,
LoungeJam.mp3, Prosonica.mp3, and playlist.xml.
There's nothing special about the MP3 files other than the fact that each contains
several ID3 tags. We'll access these files in our project.
The playlist.xml file contains basic information about these four files in a simple
structure:

<Playlist>

<Song URL="Agent00.mp3">Agent 00</Song>

<Song URL="HardCope.mp3">Hard Cope</Song>

<Song URL="LoungeJam.mp3">Lounge Jam</Song>

<Song URL="Prosonica.mp3">Prosonica</Song>

</Playlist>


The only items identified in this file are the name and the filename (in the form of
a URL attribute) for each song. This data will eventually be loaded into the
playlist_lb List component instance.
By placing the information about our MP3 files in this XML file, we can add and
delete songs simply by editing this file, and our MP3 player will compensate
automatically, as you'll soon see.

3. Return to Flash. With the Actions panel open, select Frame 1 of the Actions layer
and add the following script:
4.
5. var playlistXML:XML = new XML();
6.
7. playlistXML.ignoreWhite = true;
8.
9. var music:Sound = new Sound();
10.
11. var currentSong:String;
12.

Because our project will load the playlist.xml file, the first line of this script
creates a new XML object named playlistXML for holding the incoming file. The
next line tells that XML object to ignore any spaces within the loaded XML
document because keeping these spaces could prevent our script from accurately
locating data within the file.
The next line creates a Sound object named music into which we'll load our
external MP3 files, one at a time, as they're selected and played by the user.
The last line creates a variable named currentSong to store the filename of the last
selected song from the playlist_lb instance. The reason for this variable is that
selecting a song from the list won't cause it to load and play automatically. This
occurs only after the Play button is clicked. This variable is necessary to store the
filename of the last selected MP3 from the list until the Play button is clicked and
the file is loaded. This principle will become clearer in a moment.
4. Add the following script below the current script:
5.
6. playlistXML.onLoad = function(){
7.
8. var tempArray = new Array();

9.
10. for(var i = 0; i < this.firstChild.childNodes.length; ++i){
11.
12. tempArray[i] = new Object();
13.
14. tempArray[i].label = this.firstChild.childNodes[i].firstChild.nodeValue;
15.
16. tempArray[i].data = this.firstChild.childNodes[i].attributes.URL;
17.
18. }
19.
20. playlist_lb.dataProvider = tempArray;
21.
22. }
23.
24. playlistXML.load("playlist.xml");
25.

The last line shown here loads the playlist.xml file into the playlistXML object.
Before that action occurs, however, the first part of the script creates an onLoad
event handler so that the object knows what to do with the data after loading it.
Let's look at this event handler in detail.
It's important to remember that the data loaded in from this XML file contains the
name of each song, such as Agent 00, and the filename of the song, such as
Agent00.mp3 (go back to Step 2 if you need to review this info). Our goal is to
transfer these two pieces of data for each song in the XML file into a list item for
the playlist_lb instance, where the name of the song represents the label property
for the item, and the filename represents the data property. Because our XML file
contains four songs, our playlist_lb instance will eventually contain four items,
each with a label and a data property. Remember that the label property for an

item represents the text shown (this will be the song's name) for the item; the data
property represents a hidden value that can be accessed and used when the item
(the song's filename) is selected from the list.

×