Task 5: Streaming MP3 Audio On-demand
You want to stream audio or just load an audio file on the fly, but dedicated music/video plug-ins like RealPlayer and Windows Media are bulky. In addition, there's no guarantee that visitors will have them or want to spend a half hour downloading them.
Flash MX Solution
Although Flash Player 6 is still new software, Flash Player in general is well accepted by Web users because of its small size. Flash MX features new dynamic MP3 handling, so you can load music with script. Apart from encoding the file in MP3 format, there is no additional processing required.
(Interesting side note: unlike previous versions, Flash Player 6 will access files across different network domains. So you can load music from other sitesjust be sure to request permission first).
To script a Sound object to load streaming MP3 audio, complete the following steps:
First of all you'll need an MP3 file; any one will do. Place a copy of it into a new folder and make a note of the file name. Also ensure that the file name doesn't contain spaces (some MP3 programs, like iTunes, add spaces to file names to make them easier to read).
Open up a new Flash document, save it to the same folder as the MP3 file, and name the document MP3Player.fla.
Click the first frame on the timeline, and open the Actions panel.
Paste or type in the following ActionScript code:
myMP3 = new Sound(); myMP3.attachSound("thefilename.mp3", true);
You should substitute thefilename.mp3 in the above code for the actual name of your file. MP3 streams play as soon as enough data is available to start the decompressor, so those two lines of ActionScript are all you need. To stop playback or set the volume, use the stop() and setVolume() methods of the Sound object.