Tutorial details:
Written by: Dreamer
Difficulty Level: Intermediate.
Example (256kb) This tutorial will show you how to make a simple yet functional 'fast forward' and 'rewind' buttons for a jukebox. If you don?t know a lot about Actionscripts, don?t worry, I?ll guide you along the way and try to explain everything. However, I do recommend that you know a little actionscripting. The scripting for this doesn?t require any looping of a time line but rather the bulk of the scripts will be placed on the actual 'TextArea' component. Let?s begin. 1.   Construct yourself a very simple jukebox with the following components:
  • a 'stop' button
  • 'play' button
  • 'fast forward' button
  • 'rewind' button
  • a TextArea box
Below, I constructed a real basic one so you?ll get an idea of what you?ll need. One thing to note; to reduce file size, you can replace the TextArea component with a simple input text box and convert it into an MC. Here I'm just using a TextArea component for simplicities sake. Example (256kb) 2.   Import an mp3 of your choice into the library, right click it and go to 'linkage'. Next, click, 'export for actionscript' and give it an 'identifier' name. In this tutorial, the identifier is called, did. Design your jukebox on a single key frame and on that key frame put the following code;

baseVolume=15;
mySound=new Sound(_root); mySound.attachSound("did");
mySound.setVolume(baseVolume * 2);

This script creates a sound object that pulls the imported mp3 from the library onto the stage by the name, did. If you notice, the last 3 lines of code begins with, mySound. If you create any objects, (in this case a sound object), the name you give it must precede any identifiers, such as attachSound, and setVolume. Let?s break it down some more. The first line, baseVolume=15; is a variable called, baseVolume, (give it a unique name of your own), which has a value of 15. What this does is it set?s a volume level that will be used for the, mySound.setVolume(). However, this line of code really isn?t needed unless you?ll be doing a little more complex scripting to control the volume. But, it?s good practice to use it anyhow. The next line, mySound=new Sound(_root); begins the sound object. The name of the object is mySound (give it a unique name of your own) and the new Sound(); is the object you?re calling. Inside the parenthesis is the location of where the mySound will be attached. In this case, it will be the _root level. The third line of code, mySound.attachSound('did'); grabs the mp3 from the library by the identifier name, did, And then attaches it to mySound. It?s pretty self-explanatory. The last line, mySound.setVolume(baseVolume * 2); set?s the initial volume level of the mp3 that will be playing. Notice that baseVolume, which is the variable I created is used and then multiplied by 2. What this equation does is then sets the initial volume to 30. As mentioned above, baseVolume is not needed thus you would simply put in 30 rather than the equation. 3.   Next, click on the 'stop' button and code the following:

on(release) {
        mySound.stop(); }

This is pretty strait forward. On release of the 'stop' button, it executes the code inside its curly brackets. mySound.stop(); tells the mp3 that is playing to stop in it?s tracks. 4.   Next, click on the 'play' button and code the following:

on(press) {
        mySound.stop(); box.hold=0;
} on(release) {
        mySound.start(0,1); }

The on(press) executes the codes inside its curly brackets as the one above, but then there's the other line, box.hold=0; which clears the current "saved" position of the mp3. The reason for this is if you were to hit play and then hit fast forward until the end of the mp3, the end of the mp3 is still stored in the variable hold. So if you were to press play again, and then try to fast forward once more, the mp3 freezes. I won't be able to get into detail here since it would take up alot more writing. Le'ts just say, you do need that if you want the jukebox to function right. The on(release) executes the line of code, mySound.start(0,1); This snippet of code starts the mp3. The 0 inside the parenthesis starts the mp3 at a specified point in the song. It?s measured in seconds so if the 0 was replaced with '5', then the mp3 will start 5 seconds into the song. The numeric value is settable only to however long the mp3 is in seconds. The 1 is how many times you want the mp3 to loop. In this case, I want it only to loop once, or play one time. You?re probably wondering why I have a mySound.stop(); on a play button. Well, let?s just say if you don?t, you?ll duplicate the song over itself each time you hit the play button leaving you with an ugly audio mess. Thus, each time you hit 'play', the code will stop the song then start it over. 5.   Next comes the bulk of the coding. First, give the TextArea component a name. For this tutorial, I called it 'box'. Click on the TextArea component and code the following script:

onClipEvent(load) {
        alpha=0; }

onClipEvent(enterFrame) {
        this.text=_root.mySound.position; if(alpha==1) {
                _root.mySound.stop(); hold=_root.mySound.position*.001;
                hold++; _root.mySound.start(hold,0);
        } if(alpha==2) {
                _root.mySound.stop(); hold=_root.mySound.position*.001;
                hold--; _root.mySound.start(hold,0);
        } }

The first half of the code is onClipEvent(load) which executes the line of code inside its curly brackets. This keyword along with the load inside its parenthesis executes only once each time its MC (or component) is encountered on a timeline.
The code inside the curly brackets, alpha=0; is a variable I created called alpha which has a value of 0. This code, alpha=0; plays a very important role in controlling the fast forward and rewind abilities.
The second half of the code, onClipEvent(enterFrame) loops as long as its MC/component is under the play head. So it will continually execute the lines of code within its curly brackets.
The code, this.text=_root.mySound.position; takes the current position of the mp3 and sends it to the TextArea as text. In this.text the this refers to the TextArea since the code is on it and text refers to the TextArea identifier that enables text to be viewable. Thus, you will be able to see the constant position of the mp3 in milliseconds.
The next lines of codes are basic if statements which I won?t go into detail. But basically, it states if alpha is equivalent to 1, then it will execute the 4 lines of code inside its curly brackets.
Inside those brackets are the following:
  • _root.mySound.stop(); tells the mp3 to stop.
  • Then there is, hold=_root.mySound.position*.001; which is yet another variable I called, hold that has a value of _root.mySound.position. What this does is it will grab the position where the mp3 has stopped in milliseconds. Then the *.001 will multiply the grabbed milliseconds and equate it into seconds; (i.e. 20,000 milliseconds x .001=20 seconds). The reason for the equation to convert the milliseconds into seconds is because later, you will be grabbing the stored position of the stopped mp3 for further manipulation using the mySound.start(); (remember that mySound.start() measures starting points in seconds whereas, mySound.position grabs the position in milliseconds).
  • Then the variable hold will increase (++).
  • Finally, _root.mySound.start(hold,0); will execute, which will restart the mp3 from the last position that it was stopped. The whole first if statement executes the fast forward ability while the second if statement does the same thing for the rewind (hold--).
6.   Next, click on the 'rewind' button and code the following:

on(press) {
        if(_root.mySound.position>.001) { box.alpha=2;
        } }
on(release) { box.alpha=0;
}

Within the curly brackets of the on(press) is another if statement which says that if the current position of mySound is greater than .001 then execute box.alpha=2. Remember that you had created variables on the TextArea component called, alpha and gave it a value of 0. And also remember that you created if statements saying that if alpha is equivalent to 2 it will execute certain lines of code. Well, here you are instructing alpha to become 2, thus executing those lines of code. The reason for this snippet is that if you had no mp3 playing and you simply press the rewind button, it would automatically play that mp3. So this code will first make sure that the song is already playing before the rewind can work.
The box.alpha=0; states that on release of the button (on(release)), it will default the alpha back to 0 which will stop executing the if statements on the TextArea component. Pretty strait forward isn?t it?
7.   Finally, if you click on the 'fast forward' button, you will find the same code as the one above with the exception that box.alpha=2 is now box.alpha=1. This just executes the other if statement on the TextArea component and thus enabling the fast forward ability. At last, you have finished all the Actionscripts and hopefully there?s no errors. Of course, these codes can be greater refined to have much better and smoother control over the mp3 but, this tutorial just covers the fundamentals. Test it out and see if it works. Enjoy!