Flash: Create a Full Streaming Flash MP3 player using XML! (pt. 1)
MP3 player in Flash - No problem
MP3 player in Flash controlled by a XML file - Could be a problem
PART II of this tutorial can be found HERE
PART III of this tutorial can be found HERE
No panic, that's what I will going to show you now.
In this tutorial I will just cover the basics of the mp3 player, maybe I will create a more advanced one with some more features.
Let's get playing those mp3
Step 1
First of all we need to create a few buttons, so we can control our player ![]()
I've create the buttons using the text tool, and with the font Webdings.
Create 4 buttons and give them also the same instanceName:
Place those buttons on the stage, and give them an instacename like already mentioned above.
Furtheron, create a dynamic textfield where you will show the songname or whatever we want, give it an instanceName "display_txt" without the quotes
Create a new Layer in your movie, rename it to Actions, and put a stop action on frame 1, Actions Layer.
- Step 2
Now that we have something to control the player, we need to feed the player with something, we will use XML for that task.
Open your favourite texteditor, I use Notepad or Dreamweaver, and create a new XML file. Type following code into that XML file you created:
1 2 3 4 | < ?xml version='1.0' encoding='utf-8'?> <songs> <song /> </songs> |
Now you just need to fill the information about your songs between those 2 tags & .
Here what I've done:
1 2 3 4 5 | < ?xml version='1.0' encoding='utf-8'?> <songs> <song name="Pixel2Life Soundtrack 01" file="music/p2l_01.mp3" > </song><song name="Twodded Soundtrack 01" file="music/twod_01.mp3" > </song></songs> |
Save this newly created XML file as "playlist.xml" in the same directory as your .fla file.
Step 3
Now we need to feed the XML into flash, this will afford a lot of scripting. I will try to explain by my best.
First we create a new XML object:
1 | playlist = new XML(); |
Then we say that he should ignore whitespaces
1 | playlist.ignoreWhite = true; |
Then we create 2 global arrays for the playlist, one for the Songtitle and one for the filepath.
1 2 3 4 5 6 7 8 9 10 11 | playlist.onLoad = function (success) { _global.songname = []; _global.songfile = []; if(success) { for (var i=0; i _global.songname[i] = playlist.firstChild.childNodes[i].attributes.name; _global.songfile[i] = playlist.firstChild.childNodes[i].attributes.file; } } else {display_txt.text="Error loading XML"} } |
With this Script, I tell Flash: "If playlist loaded successful, create 2 containers and check how many songs are in there, if no XML display an Error.
at the end of this script we just need to tell flash to load the xml file we want
1 | playlist.load("playlist.xml"); |
If you want to see if everthing is loaded correctly, just place a trace action after the "_global.songfile".
1 | trace(songname[i]+" "+songfile[i]); |
this will display your array in the output window.
Step 4
Now that we have the basic functionallity of the player, let's put the music into it.
1 | _root.createEmptyMovieClip("sound_mc",1); |
Here we create a emptymovieclip that will hold our music.
1 | _root.sound_mc.sound_obj = new Sound(); |
This will place our music inside of our newly created Movieclip
1 | _root.sound_mc.songStarter(songfile[0],songname[0]); |
This bit will load and start our first song in the array.
1 2 3 4 5 6 7 8 9 | MovieClip.prototype.songStarter= function (file, name) { this.sound_obj.loadSound(file,true) // true = streaming this.onEnterFrame = function () { // onEnter Frame event if(this.sound_obj.position>0) { //When sound starts delete this.onEnterFrame; // delete the event trace("Song has started");//Just checking if everything works } } } |
Here the complete code we've created:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | playlist = new XML(); playlist.ignoreWhite=true; playlist.onLoad = function (success) { if(success) { _global.songname = []; _global.songfile = []; for (var i=0; i<playlist .firstChild.childNodes.length; i++) { _global.songname[i] = playlist.firstChild.childNodes[i].attributes.name; _global.songfile[i] = playlist.firstChild.childNodes[i].attributes.file; trace(songname[i]+" "+songfile[i]); } _root.createEmptyMovieClip("sound_mc",1); _root.sound_mc.sound_obj = new Sound(); _root.sound_mc.songStarter(songfile[0],songname[0]); } else {display_txt.text="Error loading XML"} } MovieClip.prototype.songStarter = function (file, name) { this.sound_obj.loadSound(file,true) this.onEnterFrame = function () { if(this.sound_obj.position>0) { delete this.onEnterFrame; this._parent.display_txt.text=name; } else { this._parent.display_txt.text="loading..." } } } playlist.load("playlist.xml"); </playlist> |
Now if you test your movie with CTRL+ENTER, you will see that your music is being played, and the songtitle appears on the dynamic textfield you've created.
Step 5
After a few minutes the song will stop (depending how long your track is ![]()
Now you want that the player moves on to the next song. Here what you need to do. This code will be placed inside our Prototype right after the second } that closes our function.
1 2 3 4 | this.sound_obj.onSoundComplete = function (){ (song_nr==songfile.length-1)? _global.song_nr=0 : _global.song_nr++; _root.sound_mc.songStarter(songfile[song_nr],songname[song_nr]); } |
First line is just telling "when the sound completes..."
Second line: "Pick up next song in array.."
Third line: "start the next song"
are you wondering why I've put a -1? that's because the array starts at 0.
Step 6
Are you still here? If yes, congratulations you almost made it.
Now we just need to give our buttons some functionality.
PLAYBUTTON:
1 2 | btn_play.onRelease = function () { this._parent.sound_mc.songStarter(songfile[song_nr],songname[song_nr]); |
STOPBUTTON:
1 2 3 | btn_stop.onRelease = function() { this._parent.sound_mc.sound_obj.stop(); } |
NEXTBUTTON
1 2 3 4 | btn_next.onRelease = function () { (song_nr==songfile.length-1)? _global.song_nr=0 : _global.song_nr++; _root.sound_mc.songStarter(songfile[song_nr],songname[song_nr]); } |
PREVIOUSBUTTON:
1 2 3 4 | btn_prev.onRelease = function () { (song_nr==0)? _global.song_nr=songfile.length-1 : _global.song_nr--; _root.sound_mc.songStarter(songfile[song_nr],songname[song_nr]); } |
COMPLETE CODE
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | stop(); playlist= new XML(); playlist.ignoreWhite=true; playlist.onLoad = function (success) { if(success) { _global.songname = []; _global.songfile = []; for (var i=0; i<playlist .firstChild.childNodes.length; i++) { _global.songname[i] = playlist.firstChild.childNodes[i].attributes.name; _global.songfile[i] = playlist.firstChild.childNodes[i].attributes.file; trace(songname[i]+" "+songfile[i]); } _root.createEmptyMovieClip("sound_mc",1); _root.sound_mc.sound_obj = new Sound(); _global.song_nr = random(songfile.length); _root.sound_mc.songStarter(songfile[song_nr],songname[song_nr]); } else {display_txt.text="Error Loading XML"} } MovieClip.prototype.songStarter = function (file, name) { this.sound_obj.loadSound(file,true) this.onEnterFrame = function () { if(this.sound_obj.position>0) { delete this.onEnterFrame; this._parent.display_txt.text=name; } else { this._parent.display_txt.text="loading..." } } this.sound_obj.onSoundComplete = function () { (song_nr==songfile.length-1)? _global.song_nr=0 : _global.song_nr++; _root.sound_mc.songStarter(songfile[song_nr],songname[song_nr]); } } btn_play.onRelease = function () { this._parent.sound_mc.songStarter(songfile[song_nr],songname[song_nr]); } btn_stop.onRelease = function() { this._parent.sound_mc.sound_obj.stop(); } btn_next.onRelease = function () { (song_nr==songfile.length-1)? _global.song_nr=0 : _global.song_nr++; _root.sound_mc.songStarter(songfile[song_nr],songname[song_nr]); } btn_prev.onRelease = function () { (song_nr==0)? _global.song_nr=songfile.length-1 : _global.song_nr--; _root.sound_mc.songStarter(songfile[song_nr],songname[song_nr]); } playlist.load("playlist.xml"); </playlist> |
FINISHED!!
Boys & Girls that's it for the moment, now you got your streaming player running being feeded by a XML file.
HERE you can download my .zip file with 2 sample mp3's and the working .fla
Have fun with it.
Tiago Dias



February 17th, 2009 - 01:12
@Baxter
How did you fix it?
March 27th, 2009 - 00:41
These are really great tutorials, however I cannot get this to work. I have tried the basic player and the advanced player and even tried just opening the samlpe files. The player opens and seem functional, but desn’t load the files. when I test in Flash, I get this error:
Test1 test1.mp3
Test2 test2.mp3
Error opening URL “file:///Music%20G5/Dreamweaver%20DATA/HOTSPOT/public%5Fhtml/samples/audioplayer%5F1/”
All of the files are located in the same directory, so it doesn’t seem like a path issue…
May 6th, 2009 - 20:27
Hi…
I get everything to work until i try putting the music player into a website im creating.
It previews nicely in flash but when i put that flash movie into the site it has troubles loadig the xml.
how do i get the xml to interact with the html?
May 23rd, 2009 - 14:28
Is there a way to limit the playback of each song to say 45 seconds? I need this to play only a sample of the track as a preview for people buying it. Must be pretty simple no? Thanks in advance. DV
June 4th, 2009 - 15:18
exactlly my problem, ipreviews nicely and the list of tracks does not appear on website. Though works whah i make a direct link to the player from dreamweaver. any input would be great.
June 18th, 2009 - 11:45
There’s two typos in the fla, which lead to an error. They are in the onSoundComplete code. It says songfiles instead of songfile. If you fix those it works fine and it is exactly what I was looking for, so thanks.
this.sound_obj.onSoundComplete = function() {
(song_nr == songfiles.length-1) ? _global.song_nr=0 : _global.song_nr++;
_root.sound_mc.songStarter(songfiles[song_nr], songname[song_nr]);
};
June 27th, 2009 - 12:18
I use this player and its brilliant but I need to be able to control the playlist externally with the next, and back buttons of a sling panels widget so that as the user goes through the sliding panels the song changes accordingly.
Is this possible, I would love it to be so.
Thanks in advance.
nige
July 18th, 2009 - 21:32
dude thank you so much, this is the only tutorila that got me going , i appreciate it
September 10th, 2009 - 06:36
Hi
First then all, let me give you my congratulations for this awesome tutorial… I really thought that I would need a lot of things to get this result. Great work.
I just have a little problem. When a song ends the display_txt show the name of the track but nothing happens and my browser show “conecting to …” and freezes there.
Please let me know what is the problem.
Thank you a lot
Alex K
September 10th, 2009 - 21:05
@Alex K
Hi… my mistake, I didn’t read all previus post and I didn’t saw the solution to this problem. It’s all about a couple of “s” in the sentence “songfile(s)” present at the function songfile onSoundComplete.
Thank you so much for your tutorial.
Alex K.
October 2nd, 2009 - 22:58
Thank you very much!
October 11th, 2009 - 05:58
I have read all the comments, I am sure I’ve read them so much, I’m missing something. I too am still getting the undefined error, I can not figure out what is wrong and what I am missing.
Can you please let me know what is missing?
It will play the first song, but will not play the second without any prompting from me. I am using flash 8 and dreamweaver mx
This is just driving me nuts, because I know it’s something I’m doing wrong.
Please help me
October 16th, 2009 - 01:45
thanks for this!!!!
October 27th, 2009 - 14:12
I wanted to know how to make it so it doesnt play in random order. And secondly, how to make it so when the player is loaded, it doesnt automatically start playing the first song, but rather, you have to click the play button to play it. Meaning, it is in the stop position at the beginning.
THanks
November 1st, 2009 - 06:40
@Paul Baarn
thank you!
November 4th, 2009 - 04:58
I saved everything to the same folder on my host server, i change the errors found but when I press the buttons nothing plays
November 4th, 2009 - 13:19
I did everything you said and it didn’t work. So I tried to use your files and it would work either it just said loading when I used your audioplayer on the website. http://www.nosiddainc.com/mp3test.html
November 6th, 2009 - 02:58
Change from:
this.sound_obj.onSoundComplete = function() {
(song_nr == songfiles.length-1) ? _global.song_nr=0 : _global.song_nr++;
_root.sound_mc.songStarter(songfiles[song_nr], songname[song_nr]);
};
To
this.sound_obj.onSoundComplete = function() {
(song_nr == songfiles.length-1) ? _global.song_nr=0 : _global.song_nr++;
_root.sound_mc.songStarter(songfile[song_nr], songname[song_nr]);
};
Just the the wrong name is songfile, not songfiles…
By
November 17th, 2009 - 12:37
How to make this player play a song which is already online and i should give its URL in embeded code…
December 2nd, 2009 - 17:47
I tried the basic tutorial and it worked perfectly. but when i placed the .swf, xml and .mp3 files on my local server; it really didnot worked.. the flash file seems to be functional but it doesnot plays the music. there is no path issue as i placed all the files in same folder..
please help me how it will work in local server..
quick reply will be highly appreciated..
December 9th, 2009 - 23:49
Hey,
Amazing player, thanks so much for this. It’s the second time I’ve implemented it on a website and both times it’s really done the trick.
I struggled to turn off the auto play feature – there’s a lot of people asking how and there seems to be no clear answer. The way I did it was by removing the following line of code (line 24):
_root.sound_mc.songStarter(songfile[song_nr]);
Seems to have worked, no side effects yet! I’m using the code from Part 3 of the tutorial, incidentally.
Thanks again
December 10th, 2009 - 13:49
Hi Dave,
I’ve already wrote the solution multiple times throughout all 3 pages, but it kind of sucks to scroll through all comments
But back to question, yes that is the line that you need to comment out and nope there are no side effects.
Happy you like the player.
December 10th, 2009 - 19:43
Hey Tiago,
Nice one, glad I got it right!
I must have kept missing the solution when I went through the comments before. Ah well!
I don’t suppose you ever got anywhere with making it so the playhead can be scrubbed through the song did you? That would pretty much make it my perfect player!
Cheers again, keep up the good work!
December 10th, 2009 - 20:13
Dave,
One of my clients: http://toscanorecords.com/ also requested that, but since we are “downloading” the mp3 it doesn’t make much sense to scrub the playhead (in case someone has a really bad connection). Anyway the client still wanted it and I made the playbar clickable, which is kind of easy:
You just need to create a click function on the progressbar get the mouseX position and calculate the tracks position accordingly, something like, ( songLength / progressBar.width ) * mouseX
That gives you the seconds that you need to seek to