Advanced Flash 8 Game Programming : Using Actionscript to loop mp3 files. Plus free music for your games!

While I am in the process of completing the next lesson for my Advanced Flash Game Programming set of tutorials, I have decided to take a little break and create some music loops for my games. Yesterday, via snail mail, I received a little gem I purchased called Magix Music Maker Deluxe 12. If you have no real musical talent (like me) or even if you do, this inexpensive tool kit can be used to create all manner of music and sound effects for your games. I don’t want this entry to read like a Magix commercial, so I will break that spell right now by saying that even though I have purchased every new edition of Music Maker for the last 6 years, I have always been a little disappointed with the final package. I have had all manner of problems with this German software studio – ranging from strange Blue Screens Of Death, to absent (but advertised) features to less than desirable included loop collections. BUT, I keep buying it, if only to see what new amazing feature they will add that can only be obtained for 100s of $$ else where. This edition is no different as they have added in some very cool new synthesizers such as ATMOS which will come in very handy when making background sounds for games (cool). The problem with this year’s version is with the advertised built-in version of Vital Instruments synth. It is a limited version that must be “upgraded” to get full use of (suck) The upgrade is only $15.00, but it should not be advertised a a serious feature of the deluxe version in its current state. In any case, Music Maker, Acid Music, or some easy looping or sound generating tool is a must for the “garage game” developer. You can create some pretty impressive stuff with just a few clicks, some loops and a little know-how.

The beauty of small looping sound files are just that, they take up very little bandwidth, and can add atmosphere, tension, and any number of other attributes to your games at a cheap cost. Unfortunately, you will need to be aware of some limitations before you jump up and try to create your own looping music for use in Flash. The first limitation is with the .mp3 file format. .mp3 files are great, but not for looping music. The ID3 tag information is stored in a small leader space at the start (current mp3 spec) or end (older mp3 specification). As well, the spec also has the file broken up into “frames” of other data. This will reap havoc on your attempts to loop mp3 files that are either loaded in at run time or imported to the Flash library. One solution is to only create wav formatted files that do not have any “leader” or “follower” information stored in an area that will prevent proper looping. Most good audio editor software will allow the user to export mp3, ogg Vorbis, or wav formatted files with varying bit rates and frequencies. I have found that 44K 16bit stereo wave files sound the best when imported into Flash and then exported with .mp3 format compression. These will not suffer from the dreaded ID3 tag leader space as Flash is just using the mp3 compression spec, not creating mp3 formatted files on the fly as you export.

Now, that sounds great, so you should always use wav formatted files, right? Not necessarily, because unfortunately, Flash cannot load wav formatted looping music files at run time. If your app is designed to load in music loops as needed, you will need to load in .mp3 files. If you do this, you will notice the slight pause at the end of the loop before the next sequence begins (because of the ID3 tag info, etc). This is unacceptable to the listener. Also, our job is made more difficult because implementation of the mp3 specification varies by vendor, with no one implementation being that absolute standard.

Here is my solution. We need to write a function that will monitor the playing of you music loops and when the loop ends, skip the leader (or follower as it well may be) programmatically and play the loop again. We can do this in Flash pretty easily, but you will need the right tools to get started. Basically, you need a wav editor to view the mp3’s wav form. Most mp3 files will store the ID3 tag info in 49 +milliseconds at either the beginning or end of the file. For instance, Magix Music Maker 12 exports with 49 milliseconds of ID3 information at the END of the file. This is the older mp3 spec (newer specs have it at the beginning). The MP3 file is broken up into “frames” of information. The first frame holds file data related to everything from the bit rate to copyright information. Luckily, this is usually 26 milliseconds long. Even if you don’t have ANY audio tools, you can start with the idea that you must skip the first 26 milliseconds and the last 49 milliseconds at least. If your MP3 file has all ID3 data at the beginning, then you will need to skip more to the beginning, and less at the end. The beauty is that you can easily experiment with the values in Flash and listen to the resulting playback until you get a loop that works.

One other thing to note is that we will be watching the “position in milliseconds” of the mp3 file as it plays. This is dependent on a setInterval or onEnterFrame call. If we set our movie frame rate at only 12 FPS, we will only be able to examine to position 12 times a second. If you are having trouble with loops accurately based on your frame rate, then you can do two things. One adjust the frame rate, or two, if you cannot adjust the frame rate, then you must tweak the variables for leader and follower.

Our next step is to write some simple code to do just that.

[cc lang=”javascript” width=”550″]var my_sound:Sound = new Sound();
var follower:Number=50;
var leader:Number=26;
var placeToStop:Number=my_sound.duration-follower;

**** ***
First we create an instance of the sound class to holder our looping sound.
Next we attach an mp3 from the library. I did it this way so in the example file you can just download one fla. You can just as easily load the .mp3 file from the file system at run-time.
The "follower" variable holds the number of milliseconds of time to skip at the end of the file.
The "leader" variable holder the number of milliseconds of time to skip at the beginning of the file.
The "placeToStop" variable holds the duration of the file minus the follower. We don't need a similar variable for the "placeToStart" because we always know that is 0 + the leader.

[cc lang=”javascript” width=”550″]my_sound.start(leader/1000);
var intervalVal=setInterval(runMe,1);

**** ***
The Sound.start() function allows us to set an offset to skip the leader. We specified the leader in milliseconds (26) above, but the start() function requires total number of seconds to be provided. We need to divide our 26 by 1000 to get the number of milliseconds.
Next we start our interval running and we attempt to check the sound every (1) millisecond of time. This will have varying effects based on your FPS setting. The example uses 12 FPS and functions pretty well.

[cc lang=”javascript” width=”550″]function runMe() {

if (my_sound.position > placeToStop) {



**** ***
Here is the real meat of the code and it is pretty simple, really. On each interval we check the current sound position (in milliseconds) against the "placeToStop" variable be assigned above. If the position is greater than our "placeToStop" then we stop the sound and start it up again but we first skip the leader before playing.

This code is just an example of how to write the basic functionality. The best way to extend this would be to write an LoopMP3 class. This might extend the sound class or it might use composition attach a sound to an instance of the class. Also, depending on the type of application or game and the game loop methodology, you can create either an event-based (broadcaster) style method to start and stop the sound or you can use the polling game loop style I have shown above.

I have created some example .mp3 files that you can test out, as well as an example .swf and fla for you to use. Also, I have included for of my created music loop .mp3 files for you to use in your own games.

Note: After the blog was moved these files no longer are in the same location so they won’t play in this page (with the current embed). If you have made it this far it is a pretty good idea to test it yourself with the .fla and sound files below anyway, [8bitjeff – 7/24/2010]

playmp3_loadfile.swf (2007)

This is slightly different than the code in the blog. I updated the file to load in an .mp3, wait for load completion, calculate needed data and then play the file.

You can change the file loaded to any local .mp3 file. I have included 4 of my original music loops for you to play with. You are free to use then as long as you abide by the Value Added License (see below).

If you hear and hiccups or beat repeats, you will need to change the leader / follwer values or modify the frame rate in the .fla file.

download the fla file
download rock1.mp3

download rock3.mp3

download janglerock1.mp3

download 12_1.mp3

Value Added License Agreement

We’ve run into content aggregation problems in the past. There are too many people out there that will steal instead of create. Too many people who will sell what is not theirs for a profit. Because of those people, we have instituted the value added license for all free music loops, tutorials, example .fla files, etc. Basically. it states that you are free to use this content, make money from it, etc, only if it is part of a larger project and not by simply compiling and reselling what we have provided on the site. See the license below. This should not pose any problems for any real developers or content creators out there, only  those who wish to steal and profit off of the work of others (you know who you are).

The music loops, example code files and tutorial content are provided under the 8bitrocke.comt Value Added License:

All said content, (not including full commercial songs, games, and applications) downloaded and or purchased from, is licensed to the downloader or purchaser “Royalty Free”.
The buyer or downloader of this content licenses the right from Jeff Fulton and Steve Fulton to use said content as part of their own musical productions, web sites, Flash Movies, Games, etc for profit or not.

The content must be used as part of a “Value-added” creation. You must not simply repackage content files without adding anything to them.
Also, the content cannot be resold in the current format, or placed in a compilation of content for sale in another medium without significant changes and additions that can be judged as “Value-added”

If a disagreement about what constitutes“Value-added” occurs, it is the sole digression of Jeff Fulton and Steve Fulton to determine if the use is indeed “Value-added”.
Using these loops in your own “value-added” productions, games, etc , and making a profit from such is permitted.

Transferring, or reselling these loops not as part of your “Value-added” creations and productions is prohibited

Any use of music loops, example code files and tutorial content in a creation will constitute an agreement to these terms.

If you have any questions, please contact us at Enjoy loops and sound files.

Leave a Reply