In two week ago I have some case, I have to create an Multimedia Application Presentation in Flash with ActionScript 3. In my project, I need to play some background music which are have to play automatically and random at the start up from external mp3 files, rather then play the same song at first all the time.
In this post I want to share what I got. The code are simple, to implement only need to create new folder for music files in project folder, naming the files by sequentially. so you can use the code for your projects.
In this case, I have 6 .mp3 files in folder "musics", I named the mp3 files sequels (music1.mp3, … ,music6.mp3).
Here are the Actionscript :
import flash.media.Sound; import flash.net.URLRequest; import flash.media.SoundChannel; import flash.events.Event; playsong(); //var songs:Array=new Array("music1.mp3","music2.mp3","music3.mp3","music4.mp3"); var sound:Sound; var soundChannel:SoundChannel; var randnum:uint; function playsong() { randnum=Math.floor(Math.random()*6); sound=new Sound(); var songs:String= "music" + randnum + ".mp3"; sound.load(new URLRequest("musics/"+songs)); soundChannel=sound.play(); soundChannel.addEventListener(Event.SOUND_COMPLETE,playrandomsong); } function playrandomsong(e:Event) { playsong(); }
The notes:
- in line 13 change the number of 6 with the total number of the music files you have.
- if you have your music files in different name or if you like to list the files inside the code, remove the comment mark in line 7, remove line 15 and then replace line 16 with this code.
sound.load(new URLRequest("musics/"+musics[randnum]));
Hi there, great tutorial works great in my project! I was just wondering how you would go about using play and stop buttons to play the music instead of the music playing automatically? any comments on this would be greatly appreciated!
kind regards,
Sophie
Thanks for this! I’ve used it to play a set of very short (1 to 3 second long MP3) sounds and it works very well, but it stops playing after a little while. Is there a way to keep it going forever?
@John, sorry for late reply.
I’m not sure what wrong, but it run properly in my project.
Please download this example file http://dl.dropbox.com/u/24446554/test-load-random-musics.rar and create new folder “musics” and put some mp3 file in this folder (ie. music1.mp3, music2.mp3, etc.)
Thanks for the reply. Actually, I was trying to use this to create a piece of random music, using very short MP3s, and the more MP3s I added, the more unpredictable the playback became. Partly due to the fact that there is no listener to evaluate when the sound has downloaded completely, thus truncating the beginnings and ends of the MP3s, and often making the playback stutter. One of the problems that I noticed quickly was that, as with Javascript, randomizing an array often begins with 0 and not 1, so it was getting confused when trying to call up “part0.mp3.”
I am considering importing all the short MP3s into the FLA (altogether they are smaller than 1 MB), exporting them for AS, change their classes to “part0,” “part1,” etc., but since I do not know ActionScript, I don’t know how to code it so that it plays the MP3s from the library once I’ve exported the SWF. If you would be willing to help me on this project, I would be most appreciative. It shouldn’t be too difficult to code, but I’m such a novice.
I should have said, the playback is perfectly smooth when the SWF is on the hard disk with the folder containing the MP3s, but when it’s tested on the Internet, calling hundreds of requests a minute, it can be taxing on the server, so I’d like to embed 100 short MP3s and have them play randomly from within the SWF itself.
Still no response? All I’m trying to do is to play the WAVs from the internal library, rather than from an external directory. The file would need to be downloaded completely before it was playable, but that’s the point.
Nice one this really helped
how can i make button stop pause and play