in Actionscript

Flash MP3 Player Tutorial

It’s been a little while since I uploaded a new tutorial to gotoAndLearn(). Tonight I uploaded a new one called Flash MP3 Player Part 1 which is the first in a series of tutorials which will show you how to build a full-featured music player in Flash. This first video shows you how to create and read in an XML playlist and getthem playing inside of Flash. Check it out at http://www.gotoandlearn.com.

Hope you like it!
Lee

Write a Comment

Comment

90 Comments

  1. hi lee,

    nothing new to me :) but nice :)) – will waiting for the next parts of the tutorial. one question. when you test the movie there are three songs playing – can you tell me the title of song number three ? i like the beginning of the song – thanks for that.

    greets

    dome

  2. ok – forget it :) – in german you would say “brett vorm kopf” – just looked in the zip file :) red hot … – thanks

  3. Lee,

    I am looking forward to your take on building this Flash MP3 player. I have one that I have been using on a lot of the flash projects that I do. I hope yours is better.

    Not sure if you are going to do this but it would be cool if you could some how build in a working EQ into this. I use fake ones that I animate for the mp3 player that I use now. It would be cool if it actually worked.

    Anyway, Keep up the great work. I really learn a lot from your tuts man. Your stuff makes me want to use actionscript as much as I can to animate. Cheers!

  4. Hi Lee,

    yes a working EQ would be a realy cool thing!!!

    I am truly agog of what came next!

    *Please excused my bad english, but I have an apology, I am a young student from germany^^*

    bg,
    Johannes

  5. Hi! I just followed this tutorial and I just finished part 1 of it.

    But I came across this error when I tested the movie.

    Error opening URL “file:///D|/compiled%20work/…./…../undefined”

    Are there any forums where I can read up on it?

  6. Art, what i did was made a folder within the parent directory of all my files named class and set it in the publish settings as he said then it workd. i had the same error as you. good luck. anyquestions let me know rick@rickone.com

  7. Hi. I did a search for flash MP3 tutorial and this was the first one I clicked on. Very impressed with the tutorial! I had to go to the downloads page since it looks like you are updating your site. In the downloads page, I downloaded the tutorials and watched them, and noticed that in number 3 of this set, you mention that you put some marquee things and other codes in the .zip file with that tutorial. I didn’t notice any .zip files anywhere though! I was wondering if you took them down or if they are just down for the time being or what.

    Thanks so much!

  8. Hi! thanks for the tutorials! I just tried doing this one and came across the same error message as Art described above.
    When i tested my movie, I got this

    Error opening URL “file:///Macintosh%20HD/Users…..”

    I’m not understanding the solution that Rick offers…any ideas?

    Thanks a lot!
    K-marf

  9. Lee,
    The tutorials are done at a great pace to keep up and type with and are very informative. Great work. I have to agree with Kristin though, I didn’t see any .zip files to download for this tutorial. I’d appreciate seeing it as my flash and actionscript skills are pretty minimal. Could you tell me where to get this file?

    Thanks a bunch!
    cmonster52

  10. I just finished part one of this tutorial too. I double checked all of the actionscript and it seems to look okay. However, when I try and test the movie I don’t hear any sound and I receive the following message on my Flash Output:

    Scene=Scene 1, Layer=Layer 1, Frame=1: Line 28: ‘{‘ expected function playSong():Void

    Sorry, but I don’t understand what this means? I am using Flash 5. Any help would be appreciated.

    Thanks for the hard work in creating these tutorials.

  11. First, I want to say thanks for putting these tutorials together. It’s really helpful. I just finished the first part of this tutorial and when I tested my movie no sound came out and the following message displayed on my Flash Output:

    Scene=Scene 1, Layer=Layer1, Frame=1: Line 28: ‘{‘ expected function playSong():Void

    I don’t understand what this means? I have gone through and looked at my actionscript coding a million times and everything is exactly how it is in your tutorial. I am using Flash 5. Any help would be appreciated.

  12. Hi.

    The tutorial was great but I can’t get the sound to work.
    The output error is:

    mp3player1\mp3Player.as: Line 1: ‘;’ expected

    Does anyone have any ideas on what this error means. I’ve searched the web trying to find an answer to this.

    Many thanks,

    Nik

  13. so i did you three part tutorial for the mp3 flash player and i had one question: is there a way to not have it auto play but to stop until the user actually hits the play button. also i couldn’t find the additional tutorial on how to make the audio info scroll in the player window. can you direct me to where to find that.

    THANKS!

  14. Great tutorial.
    I am getting a syntax error I can’t figure out. In my main movie (mp3Player.fla) its set up exaclty like the tutorial says with the #include “mp3Player.as” – on frame 1 of the main timeline. The error is saying – syntax error on Line 2…….but there is nothing on Line 2, so is the include staement not closed correctly or something ? Other forums say to not use the include statement but when i delete the include i get other errors……..advice please !!

  15. I get the same, and when I delete line 2, then it runs and finds 10 other errors. “Action Script 2.0 class scripts may only define class or interface constructs… ” then it shows the code from the line. Is it just that the Flash 8 action script editor can’t handle this? or did I do something?….I’m thinking I should download the script editor you were using on the tutorial :)

    Thanks, by the way. Your tutorials are great and easy to follow. I just hope I can get this up and running!

  16. just me again…solved that one, and get no errors (silly me:))
    Files are case sensitive and one was misnamed.

    Now another problem. No errors, but no sound either.

    I double checked everything, but still no sound (and yes, the volume is turned up and my speakers are on rofl)

    thanks for your help….

  17. i am following along with this tutorial but i have run across some problems with my xml file. if you had some free time could you help me with it.

    i did a syntax error and these are the errors im getting
    line 1 Unexpected ” must be followed by an operand
    line 4 & 5 Syntax error
    line 6 Operator ‘

  18. Thanks for this great tutorial!

    For those of you who posted about the “Error Opening URL…” error, I strongly suggest double checking to make sure you correctly named your file Song.as (with a capital S). This was my problem, and I was pulling my hair out for hours in frustration until I eventually figured it out.

    A couple questions…
    1) I’m also at a loss for how to make the text scroll. I’ve looked online for marquee-type tutorials, but most of them suggest tweening, which I’m not sure would work here. Any suggestions

    2) In addition to the “next” button, I’ve also added a “previous” button, but I simply don’t know enough about actionscript to make it function. Any advice here would be appreciated.

    3) Lastly, the onSoundComplete commands aren’t working for me. Whenever a song ends, it won’t go find the next song. Has anyone else had this problem?

    Many thanks!

  19. Thanks again for the great tut. However on mp3 tut #3 I get an error. Undefined URL. I have changed the published settings and checked the spelling of the Song.as it is as instructed in the tut. This is the first problem I have had with class before… any help is appreciated using Flash 8 Pro and coding in Note pad/Seipy. thanks!

  20. “First, I want to say thanks for putting these tutorials together. It’s really helpful. I just finished the first part of this tutorial and when I tested my movie no sound came out and the following message displayed on my Flash Output:

    Scene=Scene 1, Layer=Layer1, Frame=1: Line 28: ‘{’ expected function playSong():Void

    I don’t understand what this means? I have gone through and looked at my actionscript coding a million times and everything is exactly how it is in your tutorial. I am using Flash 5. Any help would be appreciated.”

    I’m getting the same error, can anyone help?

  21. Great tutoiral! I have it almost right… for some reason the song displayed is not the song playing. I’m also working on a previous button – I”m doing something wrong but working on that. I also want to add a selectable playlist but will tackle that next.

    Any/all imput/advice/help you can give would be huge!!!

    Also I could not find where to download the source files – that may answer my song/xml display issue. If you could show me where to find that it would steer me in the correct direction.

    Thank you again.
    Donna

  22. “Scene=Scene 1, Layer=Layer1, Frame=1: Line 28: ‘{’ expected function playSong():Void”

    I am having the same problem can someone please help, or point me to download the source files, would be greatly appreciated.

    Thankyou

  23. I was wondering how you would add a shuffle function to this, so it randomly will play songs. any help would be greatly appreciated.

  24. It was a great tutorial but apparently it will not load. My songs are coming up undefined. I believe this is due to the ignorewhite function and I do not have a solution to that….

  25. I LOVE THIS SITE! and it is super cool of you to not charge people to learn.. Knowledge is Free and i am really appreciative of you sincerely just trying to share it with us without thinking of making a quick buck. Thank you!

  26. Hi has anyone resolved the issue with:
    “Scene=Scene 1, Layer=Layer1, Frame=1: Line 28: ‘{’ expected function playSong():Void”

  27. Many thanks Lee for a fabulous tutorial … but I’m working with Flash MX 2004, not Flash 8. Part 1 worked perfectly, but after making the changes in the .fla and .as files for part 2 my movie no longer works. I have checked, double- and treble-checked all my coding, but cannot find anything wrong. Is it ‘just’ that I’m not writing in Flash 8?

    … oh and where did the .zip files go to that were mentioned?

    Thanks again for all your hard work and great tutorials.

  28. Thanks a lot for your video tutorials. Actually the best thing I got from this, since I knew the code part already, was how to find out about the external editor, which I am gratefull for. I have been so tired of the flash IDE for a long time, always comming back to having focus somewhere else. Also this video got me started on using classes. Thanks.

    One note though, I think it would be nice, if you would start a tutorial by showing the final result, so we know what we are working towards, and can see right away if this is what we are looking for.

  29. hi.. do you have a tutorial of how to make sounds? and how to upload it? if u have it.. can u please just send it to my email? please.. i want to know more about flash.. thank you sir..

  30. for those who have the same error.
    This is my code and i already tested it. It should work well. There is some minor changes in the xml section to make them read successfully. Hope that this will help.

    ///set up sound object
    var s:Sound = new Sound();
    s.onSoundComplete = playSong;
    s.setVolume(100);

    //set an array of the songs
    var songArray:Array = new Array();
    //currently playing song
    var currSongIndex:Number = -1;
    //load xml file
    var xmlFile:XML = new XML();
    xmlFile.ignoreWhite =true;
    xmlFile.onLoad = function(success){
    var nodes:Array = new Array();
    nodes = this.firstChild.childNodes;
    for(var i = 0; i

  31. nodes.length ; i++){
    songArray.push(this.firstChild.childNodes[i].attributes.url);
    }
    playSong();
    };

    xmlFile.load(“song.xml”);

    function playSong():Void {
    if(currSongIndex == -1 || currSongIndex == songArray.length – 1){
    currSongIndex = 0;
    s.loadSound(songArray[currSongIndex],true);
    }else{
    currSongIndex ++;
    s.loadSound(songArray[currSongIndex],true);
    }
    }

  32. The above code worked for me, but the lee’s code did not.

    Anyways just wanted to say that lee brimelow your a legend hope you get all the sucess and fortune thats coming to you =)

  33. Hey Lee cool tutorial!

    I can’t find out what’s wrong, please help:).

    When i try to test it, it just say:
    1093: Syntax error. #include “mp3Player.as”

    Thanks, Valdemar

  34. Hi Lee,

    I created a mp3 player from your tutorial but when try to insert it into my website the music doesn’t play. Could you please help? The website is still on my c drive as it is not loaded to the server yet. Do I need to have it uploaded to the server for the music to work? Also, I tried to add a equalizer to the player; have it animated but can’t get it to stop moving when music stop. Could you please help?

    Thanks,

  35. hello lee. Not used to folowoing your tutorials but i did this one and its splended. What i really would appreciate is if there was a way to put an eq with this.It doesnt matter if its a fake eq. Thanks in advance.

  36. for everyone getting errors with the tutorial code you either have syntax errors or you are using actionscript 3.0. The code works fine with action script 2.0

    Actionscript 3.0 has a few changes, for one it doesnt like the #include so just use include “filename.as” (no#)
    also void doesnt work the same way in 3.0

    Hope this helps

  37. I keep getting “1093: Syntax Error” when i try to view the .swf file. it says there is an error in the “#include “mp3Player.as”” I have no idea what to do to fix this. I am a complete beginner at Flash, please help!

  38. AH, I’ve come accross an error. Everything is working perfectly when I preview it from within Flash, but when I’ve exported the movie there is no sound playing (though the text box displays the name of the song that’s supposed to be playing). Does anyone know how I can resolve this?

    Ed

  39. Thanks for this great tutorial!

    I have been reading some of the comments, and I thought I could address some of the issues.

    For those of you trying the object-oriented approach of part 3, the file name of the class code (“Song.as”) needs to be exactly the same as the class name (“Song”)–this includes capitalization (e.g. “Song.as” not “song.as”). This was mentioned in the video but some people may have missed it.

    For the people asking about implementing ‘back’ or ‘previous’ functionality, there are various ways to implement this. I changed the “playSong” function to take a string parameter which can either be “next” or “prev”. Within “playSong,” I check to see if prev or next was passed and then advance or go back as requested. I decided to make anything that wasn’t “prev” be considered “next”, but you could also have an “else if(‘next’)….else” if you want to check all possible (error) cases.

    Also, you will need to change all “playSong()” current calls to be “playSong(‘next’)”, and add code for the prev button (nearly identical to the next button). Lastly, create a prev button.

    ————————
    Here’s my playSong() function (note I implemented the object-oriented approach in video 3):

    function playSong(dir:String):Void{
    s = new Sound(); // BUG FIX – when next song plays, sometimes ‘position’ property not updated
    s.onSoundComplete = playSong;
    s.setVolume(75);
    mute.gotoAndStop(‘on’);
    if (dir == “prev”) {
    if (curSong == 0){ //last song in array
    curSong = sArr.length – 1;
    s.loadSound(sArr[curSong].url, true); // boolean says TRUE – plays while streaming
    } else {
    s.loadSound(sArr[–curSong].url, true);
    }
    } else {
    if (curSong == sArr.length – 1){ //last song in array
    curSong = 0;
    s.loadSound(sArr[curSong].url, true); // boolean says TRUE – plays while streaming
    } else {
    s.loadSound(sArr[++curSong].url, true);
    }
    }
    trackInfo.text = sArr[curSong].track;
    playPause.gotoAndStop(‘pause’);
    }
    ———————————-
    Here’s the prev button code

    // Prev Button
    prev.onRollOver = function(){
    this.gotoAndStop(‘prevOver’);
    }

    prev.onRollOut = prev.onReleaseOutside = function(){
    this.gotoAndStop(‘prev’);
    }

    prev.onRelease = function(){
    this._parent.playSong(“prev”); // ‘this’ = next so _parent takes us back to _root
    }

    ———————-

    I hope this helps some of you. Thanks again Lee for a great service!

  40. I’m also wondering how to forgo the autoplay. If anyone has a solution, I’d appreciate it.

    And agreed, a very nice tutorial indeed.

  41. hey, great tutorials, but I found a few problems of my own, that “Rob” posted. For the previous script, you need toedit the playSong() to playSong(‘next’), which rob stated, but if you copy and paste the following, you need to change all ” and ‘ before you continue, since they do not paste properly! Another thing, an extra “-” was removed during the copy and pasting, just did it to me now! So watch out

    Here’s my playSong() function (note I implemented the object-oriented approach in video 3):

    function playSong(dir:String):Void{
    s = new Sound(); // BUG FIX – when next song plays, sometimes ‘position’ property not updated
    s.onSoundComplete = playSong;
    s.setVolume(75);
    mute.gotoAndStop(’on’);
    if (dir == “prev”) {
    if (curSong == 0){ //last song in array
    curSong = sArr.length – 1;
    s.loadSound(sArr[curSong].url, true); // boolean says TRUE – plays while streaming
    } else {
    s.loadSound(sArr[-–curSong].url, true);
    }
    } else {
    if (curSong == sArr.length – 1){ //last song in array
    curSong = 0;
    s.loadSound(sArr[curSong].url, true); // boolean says TRUE – plays while streaming
    } else {
    s.loadSound(sArr[++curSong].url, true);
    }
    }
    trackInfo.text = sArr[curSong].track;
    playPause.gotoAndStop(’pause’);
    }
    ———————————-
    Here’s the prev button code

    // Prev Button
    prev.onRollOver = function(){
    this.gotoAndStop(’prevOver’);
    }

    prev.onRollOut = prev.onReleaseOutside = function(){
    this.gotoAndStop(’prev’);
    }

    prev.onRelease = function(){
    this._parent.playSong(”prev”); // ‘this’ = next so _parent takes us back to _root
    }

    Other then that, it looks and works fine!

  42. I followed this along in Flash CS3 and encountered some significant problems that I can’t seem to get around and would appreciate some help. First I had to switch backwards from ActionScript 3 to AS2, but once I did that the debugger keeps insisting that a) the for loop is missing a closing parenthesis (the existence of which is underscored by its presence in the line it cites in the debug message) and b) on multiple lines (starting with the first line where we declare a variable) that “ActionScript 2.0 class scripts may only define class or interface constructs”.

    What’s missing here? Or is it time to revisit this tutorial series with the new version in mind, since AS 3.0 doesn’t allow for “include” any more?

  43. Just to show the lengths I have gone to to try fixing this before posting, I have followed up each and every one of the comments above (where applicable to tutorial one) with an iterated test and cannot find the problem. All the files are properly capitalized, I copied and pasted the code section pphm posted and got the exact same error set when I tried to test the movie or tried to format the script.

    For completeness, this is happening on a new install of Flash CS3 Professional on a new MacBook running OS X 10.5.2.

  44. Hi Lee first off..awesome tutorial!

    ..okay’ i’ve tried everything including
    publish settings class path, but STIll getting url “undefined” message.
    thought maybe a coding error somewhere, so let’s try lee’s player
    (player2zip) find problem fer shure but SaMe thing Undefined URL!
    add class path to new player, doneit didit.
    can anyone PLEasee help me with this been trying for 2 days!going iNsAnE lol
    working in CS3 flash pro and windows Xp Media Center, if that’s any help
    also all files are in the same directory respectively..of course..
    Any and all help would be much appreciated; thanks.
    signed, Billy

  45. oh yeah.. player works when i modified lees’s code with pphms code ‘post 47,48), but when I add function buttons, the same “Undefined” error returns..

  46. Hey,

    Very great tutorial, learned alot from it. I have no errors but I do have a problem however.

    When the song finishes playing, it does not advance to the next song automatically. However if i click the “next” button then it loads perfectly fine.

    Any idea as to why I can’t get the player to automatically advance to the next song?

    Thank you in advance.

  47. Great tutorial. The player works beautifully. I’ve implemented it on a site I’m building. What I’d like to know is if there is a way to check if the audio is actually playing. Since I’m making the player stop when the user activates other sounds on particular pages, when I go back to areas on the site in which I want the mp3 player songs to play, it is necessary to check if the audio needs to be restored or not. Thanks in advance for any help.

  48. hey lee thanks for that but when i try to view it it comes up with veriouse erros i dont understand where you had got the XML file from called songs in the first place? could you contact me on my email address and give me a hand as its stressing me out and i would like to do it for a school project.
    thanks waseem = waseem7744@hotmail.co.uk,

  49. Hi Lee,

    a great tutorial indeed but I’m not figuring out if it works with as2 or 3? I just started but noticed that “child tag” in the action script.
    I am not familiarized to ActionScript that much but I’m not sure if this tag has only apeared in AS3…

    Would anyone tell me wich version it runs before I return to the tutorial please. I need a AS2 version and don’t want to waste the time ;)

    thx, blackbyrne

  50. Problem solved!
    I just finished the tutorial… it didn’t run first but then I found the mistake and now everything works fine! Thx!!

    Tomorrow I’ll integrate a stop/play -button and volume controlpanel…

    Let’s see if that works as well! ;)

    Greetz, blackbyrne

  51. This is the error I get… Error opening URL ‘file:///C|/Documents%20and%20Settings/Kyle/Desktop/kylie%20pics/class/undefined’
    Error opening URL ‘file:///C|/Documents%20and%20Settings/Kyle/Desktop/kylie%20pics/class/song.xml’

    Can anyone help please?

  52. This is the error I get:
    Error opening URL ‘file:///C|/Documents%20and%20Settings/Kyle/Desktop/kylie%20pics/class/undefined’
    Error opening URL ‘file:///C|/Documents%20and%20Settings/Kyle/Desktop/kylie%20pics/class/song.xml’

    Can anyone help please?

  53. Hey Lee,

    Looks like you get your work cut for you from all these forums, and questions. Luckily I have another one for you. :)

    I’m getting a “5007: An ActionScript file must have at least one externally visible definition.

  54. Hi,
    This is a really great tutorial, I am really looking forward to parts 2 and 3. However I am also experiencing the :Void error.

    If I remove :Void from the .as script, I get no errors, but I also get no sound.

    I am using Flash 5, could this be the issue?
    Any other thoughts?

    Any help would be very much appreciated.

  55. This is a great tutorial everything works fine as far as Actionscript 2.0, What would I need to change to get this all working in Actionscript 3?

    Thank you very much in advance.

  56. for anyone having the undefined error with no sound. i solved this problem by adding after each mp3 listing like this:

    I am using Flash 8

  57. anyone has any suggestions on how to fix the class path thing? tried to manually point it to the right folder but still no go

  58. ok got it, to all those who are getting the error opening url: … undefined

    make double triple sure the files named are correctly in sync

    reflected file names should be

    Song.as (case sensitive!)
    song.xml
    mp3Player.as

    good luck!