Welcome To COMPEWT Bringing you video tutorials and free resources for your "compewting" endeavours. Check Out the latest Flash Tutorial.

6 November 2009

How to create a Flash MP3 player

These are video tutorials on how to create an Mp3 in flash with an xml playlist.
If you have come from youtube the code you are looking for is below the videos.









stop(); //stops the frame from starting over and over.
playlist = new XML(); //creates a new xml object
playlist.ignoreWhite = true; //self- explanatory (makes it ignore whitespace)
playlist.onLoad = function(success) { //creates 2 global arrays for the playlist, one of the song title and
    if (success) { // cont. one of the file path.
        _global.songname = [];
        _global.songband = [];
        _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); // generates a new movie clip
    _root.sound_mc.sound_obj = new Sound(); // makes our new movie clip hold the song
    _global.song_nr = random(songfile.length);
    _root.sound_mc.songStarter(songfile[song_nr], songname[song_nr]); // this bit loads the song
};
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.text_song.text = name;
        } else {
            this._parent.text_song.text = "loading...";
        }
    };
    this.sound_obj.onSoundComplete = function() { // when the sound completes
        (song_nr == songfiles.length-1) ? _global.song_nr=0 : _global.song_nr++; // load the next song
        _root.sound_mc.songStarter(songfiles[song_nr], songname[song_nr]); // play the next song
    };
};
button_play.onRelease = function() {
    this._parent.sound_mc.songStarter(songfile[song_nr], songname[song_nr]); // function of the play button
};
button_stop.onRelease = function() {
    this._parent.sound_mc.sound_obj.stop(); // function for the stop button ... you can work the others out
};
button_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]);
};
button_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");  // reference to the playlist





The download link for my version :)
http://www.2shared.com/file/8962356/8c13f28a/player_-_Copy.html

Thanks you for viewing this tutorial :)

21 comments:

Anonymous said...

Umm,.. great tut .. but how can you make the mp3 continue to the next song all by itself .. as it is now - it stops when the mp3 is completed .. Thanks- for listening ..

Unknown said...

Thisis a great tutorial. I was hestitant to try because a few people on youtube said you were too fast but hey pause goes a long way so i managed. thank you.

The player starts automatically playing the first song but no other songs play. After the first song completes the player reads "undefined" and there is no sound. If I press the previous or forward buttons while song one is playing the song starts over again.

Could you help me please.

Thanks!
Sophie

Wild Lion said...

_root.sound_mc.songStarter(songfileS[song_nr], songname[song_nr]); // play the next song

in that line remove the S that is with caps in the word songfiles so as when the current song ends and the next song load it plays too :D

*Its tiny typo mistake but sum ppl might think it's bug so, just wanted to help :D


//wild939 <<Youtube account

Wild Lion said...

_root.sound_mc.songStarter(songfileS[song_nr], songname[song_nr]); // play the next song

Just Remove the capital S in songfileS in that line. i know it's just silly typo mistake but i wanted to make sure cuz sum ppl might think it's bug

Thanks

wild939 << youtube name

George Butter said...

I'm going to carry this tutorial forward with more features, I'll post a new video which will answer all of your comments and I'll let you know hear and on youtube.

Paul said...

Hi,

Thank you for this tutorial. I am having one problem, though.

I know it must be the path I'm trying to use for the mp3. I have all of my files (playlist.xml, player.fla, player.swf, hangwithme.mp3) in a folder called "player". This is what I have in my playlist.xml:







I keep getting error messages like this when I try to play the movie:

Hang With Me by Beth Zaje hangwithme.mp3
Error opening URL 'file:///C|/Documents%20and%20Settings/Paul/Desktop/player/hangwithme.mp3'

What should the path be? Is that why I am getting the error and why it won't play??

Thank you so much!!

Paul said...

Hello again,

You could email me at paulmeherjr@gmail.com
if you would like.

Thanks again!
Paul

George Butter said...

Hey Paul, for the benefit of anyone else who has this problem I'm posting the solution here.

as long as they are in the folder you can have your path as simply:

'hangwithme.mp3'

Unknown said...

Hi!

Thanks a bunch for this, it's exactly what I was looking for! It works great, but I have that little 'undefined' glitch when the last song ends. I was also wondering how I could make the playlist play over and over.

Cheers!

George Butter said...

I did know how to do this and I sent it to someone else, but I have forgotten now. give me a day or so and i'll get back to you :)

Brett said...

Paul,

I had the same issue, please confirm the *.xml file reads as follows - including ALL quote marks and relevant formatting.

Anonymous said...

Hey. The player is great. But I've noticed one problem. I want the player at startup could always automatic play the first song from playlist. How can I do this? Please help at pablos83@o2.pl. Thanks.

Anonymous said...

is he using actionscript 2.0 or 3.0?

Jamesceri said...

I had a problem with the player continuing to the next song. I corrected the typo (removing the 'S'), but this still did not work. Finally discovered that in onSoundComplete you need to add _global. in front of the song_nr, songfile, & songname variable.

Works a treat now. Thanks for the tutorial...


this.sound_obj.onSoundComplete = function() { // when the sound completes
trace("songStarter: onSoundComplete");
//delete this.onSoundComplete;
(_global.song_nr == _global.songfile.length-1) ? _global.song_nr=0 : _global.song_nr++; // load the next song
trace("songStarter: onSoundComplete: "+_global.songfile[song_nr]+", "+_global.songname[song_nr]);
_root.sound_mc.songStarter(_global.songfile[song_nr], _global.songname[song_nr]); // play the next song

};

Jamesceri said...

Actionscript 2.0

4EspadasRecords said...

Great video... helped out alot but i do have one question. How do i make the music play only when i click play? I don't want the first song or any other song to start automatically.
Can you please help me with that?

my email is
espada.records@yahoo.com

Anonymous said...

thanks keep going

Anonymous said...

Hi! I really really hope you read this because I just want to thank you so much! At first I was having problems but I finally fixed every detail that was wrong (LOL) and it finally works! THANK YOU SO MUCH! I don't even think you understand how much this means to me. (':

Anonymous said...

hi,

i have some problem with to play this player in a internet! ... i not works!?:

http://www.fileden.com/files/2007/2/27/828381/T_player.swf?file=http://www.fileden.com/files/2007/2/27/828381/T_playlist.xml

may i has error or this?

Alex WhyBull said...

hey so any chance you will be making a tutorial helping someone to understand the action script in general as far as what to replace with what and so forth

Jay said...

hi i keep running into a problem when i try to view the player. the output comes up as - Error opening URL 'file:///C|/Documents%20and%20Settings/JEFFREY%20WILLIAMS/My%20Documents/cypher%20media/jurassicplayer/undefined'

Post a Comment

Please keep it clean :)