Ok, so this will elicit a "duh" out of my Flex 2 experienced readers, but I was pleasantly surprised to find that Flex 2 (to be clear, ActionScript 3) can read the ID3 tags out of MP3 files. I knew that I could play MP3s in Flex, but I didn't realize I could also dig out the ID3 information.
Like most things in Flex 2, it is incredibly easy. First you load a MP3:
sound = new Sound(new URLRequest("/music/80s/EURITHMIX-SWTDREMS.MP3"));
That's a relative URL there to an Apache virtual alias on my local server. Next you add an event listener:
sound.addEventListener(Event.ID3, onID3);
This basically says to run the onID3 function when the ID3 event fires. My demo onID3 method looked like so:
function onID3(event:Event):void {
myid3info.text += sound.id3.songName + "\n";
myid3info.text += sound.id3.artist + "\n";
}
Where myid3info was a simple text control. Flex supports all the typical properties (album, artist, song name, etc). More information may be found at the Livedocs page.
I'd post a demo, but the last thing I need is a RIAA lawsuit. ;) FYI - I discovered this gem in the ActionScript 3.0 Cookbook.
Archived Comments
Note that (if I'm remembering correctly) ID3 information only works for runtime-loaded files. Embeds don't have the ID3 data.
Interesting. I wonder why it would NOT be supported. I guess it isn't a big deal since you are embedded a _known_ MP3.
Reading ID3 tags has actually been available for some time now, I think since one of the Flash 6 revisions. It's always nice to find ActionScript features you didn't quite expect to be there :)
Lot's of good stuff in that there ActionScript 3.0 Cookbook! .. or so I hear ;-)
Hey Darron - I absolutely love it so far. I did notice one problem though. In 15.2 "Starting and Stopping a Sound", the text says:
The close() method of the Soutn object not only stops the sound from playing, but also stops the steeaming... The close() method should be called only when you are sure you are finished with that particular sound. Recipe 15.1 discusses a way to stop the sound from playing w/o stopping the stream.
However, in 15.1, there is no mention of this. The only mention of this is: "See Recipe 15.2 for information on how to control a sound's playing."
Seems like an infinite loop. ;)
Just to let you know, Actionscript 2's sound object can also retrieve ID3 tags. It can read either ID3 1.0 or ID3 2.0. If your MP3 has ID3 2.0 tags, you can get just about any information out of the file you need. This functionality has been around for a while.
Flex is introducing me to all this new stuff in AS - new to me anyway. :)
Hi Ray,
There isn't a direct way to stop a sound but still have it load in the background (i.e., there is no "pause" method on the Sound class).
However, to make sonud stop while still having the stream load you would write code like the following (pseudo-code, not tested)
// Save the sound channel after you tell the sound to play
var channel:SoundChannel = sound.play();
// Tell the channel to stop playing, but record the offset
// time so that you can resume playing later
var position:Number = sound.position;
channel.stop();
// Then, continue playing
sound.play( position );
The sound should continue to stream since only the channel was stopped and the sound was never closed.
Hope that helps...
By the way, that refer to 15.1 looks like it was a typo, and should've actually sent you to 15.10 (oops!). The "Pausing and Restarting a Sound" covers the topic in a little more detail than my previous comment.
Thanks Darron!