News:

If you need instructions on how to get through the hotels, check out the enclosed instruction book.

Main Menu

Actsionscript Academy Session 8

Farted by RomanClock, July 16, 2008, 07:36:17 PM

Previous topic - Next topic

0 Members and 0 Guests are viewing this topic.

RomanClock

Actsionscript Academy:
Session 8

Using actionscript to control sound and load external music

-AS2 Version-
Using actionscript to control sound allows much more flexibility and dynamic control. Streaming and event become encumbered by framerate and are anchored when the swf is published. By using actionscript, you can dynamically control volume and panning, along with millisecond precision for tracking position and duration along with more accurate looping. In the case of loading external music, you can read the id3 of the MP3 and display the title, artist, and various other factors, including bytes loaded and total bytes.

New to actionscript? Click Here!

Programming Dictionary

this.createEmptyMovieClip("mcSound",1)
This will create a movieclip to contain the sound. This is important because when multiple sounds are playing and you only want to change one of them (whether to stop it or change volume) it needs to be associated with a movieclip. If it is not coupled with a movieclip, any references to alter it will affect EVERY SOUND and not just that one. You do not have to deal with this movieclip after you associate it with the sound object.
Part Details:
"mcSound" is the name of the movieclip
1 is the depth of the movieclip (depth is like layers but for actionscript)

mySound= new Sound(mcSound)
This creates the sound object. You will use this to reference the sound when you want to change one of its properties.
Part Details:
mySound is the name you will use for reference
mcSound is the movieclip it is associated with

Time to import music or sound!
1. Click File>Import>Import to Library
2. Select the music or sound you with to use
3. Once complete, go to your library (Ctrl+L)
4. Right click the music/sound and select Linkage
5. Click the Checkbox/bubble for Export for Actionscript
6. Make sure the Export for First Frame is also checked.
Details: This box means that the music will be loaded as if it were on the first frame of the movie. In this case, we want it to be loaded in the first frame since it is the only frame. However, in a normal situation, it is probably better to create a movieclip on another frame, and inside that movieclip make the first frame have stop(); on it, and on the second frame insert the music in event form. If you have more than one sound, make more frames in that movieclip and put the sound in event form in those frames
7. In the box next to Identifier, type "soundID" (no quotes)
8. Click Ok

mySound.attachSound("soundID")
This attaches the sound you just imported to the sound object.
Note: The id3 option is also supposed to work with attachSound, however I have not been able to get this work work, or if it even does really work.
Part Details:
mySound. is the reference to the sound object
"soundID" is the Identifier you gave your sound in the library

mySound.start()
This is the command that makes the music or sound play. The command start() can also start the music at a specific time and can also make the music loop a certain number of times.
start(secondsOffset, loops)
Both options are optional (not required).
secondsOffset is the number of seconds into the music/sound you want it to start at.
Loops is the number of loops you want it to do.
Part Details:
start() is the command to start the music or sound

mySound.stop()
Stops the sound object if playing.
Part Details:
stop() the command to stop a sound


Library Sound Example:

this
.createEmptyMovieClip("mcSound", 1);
mySound = new Sound(mcSound);
mySound.attachSound("soundID");
mySound.start();


mySound.setVolume(100)
This is how you set the volume for the music/sound. The number (100) is a percent of the original volume. This means that 0 means no sound and 200 is twice as load. The maximum volume is potentially infinite, but as it gets louder, the quality of the sound becomes less, making it into a kind of white noise.
Note: Technically speaking, pan sets the volume of the channels in the MP3, which translates to different speakers
Part Details:
setVolume(number) the command to set the volume to the percent number
100 is the percent it is setting it to, starts at 0, 100 is normal

mySound.setPan(100)
This is the command to set the pan, meaning what side of a stereo speaker system the sound will be coming from. The default pan, equal in both speakers, is 0. To make the sound come only out of the right speaker you would use 100, and for only the left would be -100. You can set the numbers to be between 100 and -100 to have the sound come out louder in one speaker and softer in a another. This is how you can make passing sound effects, as if you were moving by something. Going beyond 100 or -100 will result in various problems such as increased volume or decreased quality.
Part Details:
setPan(number) is the command to set pan with 100 as only right side speaker, -100 as only the left side speaker, and the default 0 to come out of both speakers with equal volume.
100 will set the pan to right speaker only (100 right, 0 equal, -100 left)

mySound.loadSound(url, true)
This is the command to load an external MP3. The url is the location of the mp3 or a page that directs to the MP3, meaning that if you clicked that link it would download the music. The true indicates that it is a steaming type sound, false would mean that it is an event type sound, however there is no quality difference, but when true it will start playing the sound right away.
Part Details:
loadSound(url, boolean) is the command to load an external sound
url is the location of the MP3
boolean means true for streaming or false for event (Boolean means it is either true or false)

External Sound Example:

this
.createEmptyMovieClip("mcSound", 1);
mySound = new Sound(mcSound);
mySound.loadSound("http://www.newgrounds.com/audio/download/148108", true);


mySound.position and mySound.duration
These to properties are helpful if you want to go to a specific time into the song or just know how long it is. Position is the time in milliseconds that the sound has played from the beginning. Duration is how long the entire sound is in milliseconds. Remember, these properties are measured in milliseconds, so if you want to convert them to seconds, simply divide by 1000.
Part Details:
position the position of the sound in milliseconds from the start
duration the total length of the sound in milliseconds

How to make a Pause button:
A pause button can be very useful since it will allow you to stop the sound but you won't have to restart it.
1. Create a pause and play graphic and make them into movieClips (not buttons)
2. Give instance names to the pause and play buttons (Example: pa for pause, pl for play)
3. First, use the information you've learned above and incorporate a sound. (Import, Link, Attach)
4. Now add this to the actionscript on the frame:
holdTime = 0
pl
.onRelease = function() {
mySound.start(holdTime/1000);
};

Note: holdTime will be the variable that tracks the time in the song.
This completes the play button and allow the song to play. Right now, it is set to start at 0 milliseconds (holdTime=0)
5. Next, we add the code for the pause button:
pa.onRelease = function() {
holdTime = mySound.position;
mySound.stop();
};

Now when pause is pressed, holdTime stores the position of the sound and then stops the music. Now that the position of the sound is recorded, you can use it to start the music where you stopped it. This was already put in place with the first set of code where mySound.start(holdTime/1000). The variable holdTime is divided by 1000 because position is measured in milliseconds while the parameter (the value you need to use) for start() needs to be in seconds.

Full Code:
holdTime = 0;
pl.onRelease = function() {
mySound.start(holdTime/1000);
};
pa.onRelease = function() {
holdTime = mySound.position;
mySound.stop();
};


How to make a Position Slider:
Note: These steps are ONLY for the position slider
1. Draw a horizontal line or a bar to be the slide area. Make it a movieclip and, in this example, give it the instance name br.
Note: When you make this bar, you want the cross to be on the far left of the drawing. If it is not there, move it there now.

2. Draw a slider for the bar. Make it a movieclip and, in this example, give it the instance name sl.
Note: Unlike the last movieclip, the cross should be in the center.
3. Open up the actionscript panel for the frame and we'll start with this code:
slideMode = 0
timer 
= 0
sl
.onPress = function() {
slideMode = 1;
};
sl.onRelease = function() {
slideMode = 2;
};
sl.onReleaseOutside = function() {
slideMode = 2;
};

Part Details:
slideMode = 0 is the initial value of what mode the slider is in
timer = 0 will be used to prevent the slider from jumping positions
sl.onPress will activate when the slider is pressed
slideMode = 1 activates mode 1 of the slider (in this case tracking the mouse)
sl.onRelease activates when you release the mouse one the slider
sl.onReleaseOutside activates when the mouse is release off the slider
slideMode = 2 activates mode 2 of the slider (restart the music at new position)

4. Now you add the code:
onEnterFrame = function () {
positionSlider()
};

This will mean the function positionSlider will be active every frame.
5. Now you add the function positionSlider:
function positionSlider() {
if (slideMode == 1) {
sl._x = _xmouse;
sl._x = Math.max(br._x, Math.min(br._x+br._width, sl._x));
} else if (slideMode == 2) {
ss.stop();
ss.start((sl._x-br._x)/br._width*(ss.duration-1)/1000);
slideMode = 0;
timer = 2;
} else if (slideMode == 0 && timer<1) {
sl._x=br._x+ss.position/ss.duration*br._width
} else {
timer--;
}
}

Now I will explain each section:
if (slideMode == 1) {
sl._x = _xmouse;
sl._x = Math.max(br._x, Math.min(br._x+br._width, sl._x));
} else

if (slideMode == 1) { checks if the slideMode is equal to 1, if it is, the commands below are executed. If not, it will check the next if statement after the else. This mode makes the slider follow the mouse.
sl._x = _xmouse; tells the slider to move to the _x position of the mouse
sl._x = Math.max(br._x, Math.min(br._x+br._width, sl._x)); this is a combination of two Math functions. Math.max(value1, value2) will pick which of the two values is larger. Inversely, Math.min(value1, value2) will pick the smaller of the two. When combined, these functions will create a specific area the slider can move in. The current settings are from the _x position of the bar and the _x position of the bar plus the bars width.
if (slideMode == 2) {
ss.stop();
ss.start((sl._x-br._x)/br._width*(ss.duration-1)/1000);
slideMode = 0;
timer = 2;
} else

if (slideMode == 2) { checks if the slideMode is equal to 2, if it is, the commands below are executed. If not, it will check the next if statement after the else. This mode sets the music to play at the position of the slider.
ss.stop(); stops the music from playing
ss.start((sl._x-br._x)/br._width*(ss.duration-1)/1000); evaluates the position of the slider relative to the bar as a percentage and starts the music at that time. (sl._x-br._x)/br._width creates the percent value that will be used. *(ss.duration-1)/1000 multiplies it by the total time of the music. There is a -1 because you cannot start a sound at the end, and it is /1000 because duration is measured in milliseconds while the start() parameter requires a value in seconds.
slideMode = 0; sets the sliderMode back to 0 (follow position)
timer = 2; sets the timer to 2 frames so the slider does not jump due to the position of the music being moved.
if (slideMode == 0 && timer<1) {
sl._x = br._x+ss.position/ss.duration*br._width;
} else {
timer--;
}

if (slideMode == 0 && timer<1) { checks if the slideMode is equal to 2, if it is, the commands below are executed. This mode makes the slider follow the position of the music. It also checks the timer to make sure its less than 1.
sl._x=br._x+ss.position/ss.duration*br._width }else{ timer--; if the previous statement is true, it will move the slider, if not, then it will subtract from timer. sl._x=br._x+ss.position/ss.duration*br._width moves the slider based on the position of the music. ss.position/ss.duration is the percent complete the music is at. When you multiply it by the width of the bar and add the _x value of the bar, the slider will move along the bar as a percent of the music (ie if the music is at 60% done, the slider will be 60% across the bar)


[flash]http://img151.imageshack.us/img151/5574/positioncontrols1cc5.swf width=190 height=30[/flash]
this.createEmptyMovieClip("musicMC", 1);
ss = new Sound(musicMC);
ss.attachSound("music");
holdTime = 0;
slideMode = 0;
timer = 0;
pl.onRelease = function() {
ss.stop();
slideMode = 0;
ss.start(holdTime/1000);
};
pa.onRelease = function() {
holdTime = ss.position;
ss.stop();
};
st.onRelease = function() {
holdTime = 0;
ss.stop();
};
sl.onPress = function() {
slideMode = 1;
};
sl.onRelease = function() {
slideMode = 2;
};
sl.onReleaseOutside = function() {
slideMode = 2;
};
onEnterFrame = function () {
positionSlider();
};
function 
positionSlider() {
if (slideMode == 1) {
sl._x = _xmouse;
sl._x = Math.max(br._x, Math.min(br._x+br._width, sl._x));
} else if (slideMode == 2) {
ss.stop();
ss.start((sl._x-br._x)/br._width*(ss.duration-1)/1000);
slideMode = 0;
timer = 2;
} else if (slideMode == 0 && timer<1) {
sl._x = br._x+ss.position/ss.duration*br._width;
} else {
timer--;
}
}



How to make a Volume Slider:
1. Copy the line movieclip you made for the position slider and give it the instance name vb.
2. Draw a slider for the bar. Make it a movieclip and give it the instance name vl.
3. Open up the actionscript panel for the frame and we'll start with this code:
volMode = 0
vl
.onPress = function() {
volMode = 1;
};
vl.onRelease = function() {
volMode = 2;
};
vl.onReleaseOutside = function() {
volMode = 2;
};

Part Details:
volMode = 0 is the initial value of what mode the slider is in
vl.onPress will activate when the slider is pressed
volMode = 1 activates mode 1 of the slider (in this case tracking the mouse)
vl.onRelease activates when you release the mouse one the slider
vl.onReleaseOutside activates when the mouse is release off the slider
volMode = 0 activates mode 0 of the slider (stop following the mouse)

4. Now you add the code (if you already have onEnterFrame, then just add volumeSlider() inside it):
onEnterFrame = function () {
volumeSlider()
};

This will mean the function volumeSlider will be active every frame.
5. Now you add the function volumeSlider:
function volumeSlider() {
if (volMode == 1) {
vl._x = _xmouse;
vl._x = Math.max(vb._x, Math.min(vb._x+vb._width, vl._x));
ss.setVolume((vl._x-vb._x)/vb._width*200);
}
}

Now I will explain each section:
if (volMode == 1) { tests if slider is active
vl._x = Math.max(vb._x, Math.min(vb._x+vb._width, vl._x)); restricts the slider to between the _x value of the line and _x value plus its _width.
ss.setVolume((vl._x-vb._x)/vb._width*200); converts the _x value of the slider into a percentage of 200 and sets the volume to that percent. This slider will allow a volume setting from 0% to 200% (and you can make it go to a greater percentage, but the sound quality will deteriorate)


[FLASH]http://files.myfrogbag.com/bwdehj/volumeControls1.swf width=190 height=30[/FLASH]
this.createEmptyMovieClip("musicMC", 1);
ss = new Sound(musicMC);
ss.attachSound("music");
holdTime = 0;
pl.onRelease = function() {
ss.stop();
ss.start(holdTime/1000);
};
pa.onRelease = function() {
holdTime = ss.position;
ss.stop();
};
st.onRelease = function() {
holdTime = 0;
ss.stop();
};
vl.onPress = function() {
volMode = 1;
};
vl.onRelease = function() {
volMode = 0;
};
vl.onReleaseOutside = function() {
volMode = 0;
};
ss.setPan((vl._x-vb._x)/vb._width*200-100);
onEnterFrame = function () {
volumeSlider();
};
function 
volumeSlider() {
if (volMode == 1) {
vl._x = _xmouse;
vl._x = Math.max(vb._x, Math.min(vb._x+vb._width, vl._x));
ss.setVolume((vl._x-vb._x)/vb._width*200);
}
}



MORE TO COME
-Pan control (left or right speaker) SLIDER
-ID3 for external MP3's
lemayo lol :soups:

patriotclock

Is this only for .mp3 or can .wav work too

RomanClock

Quote from: PatriotClock;1365136Is this only for .mp3 or can .wav work too

wav's will work from the library just like mp3's but they cannot be loaded externally.
lemayo lol :soups:

RomanClock

Quote from: PatriotClock;1365136Is this only for .mp3 or can .wav work too

wav's will work from the library just like mp3's but they cannot be loaded externally.
lemayo lol :soups:

AstronautClock

this is very good! looking forward to seeing more about this

Isison

I teach actionscript without the script. Yes, pure action, baby!

Actually, I teach ActionScript 3

RomanClock

Added setVolume() and setPan()!
lemayo lol :soups:

RomanClock

Added setVolume() and setPan()!

Cmon guys,lets get some posts in here, questions, help, comments, ideas?
lemayo lol :soups:

TapedeckClock

Quote from: RomanClock;1366274ideas?

there should be a button on the player where you can change and lock your player skin.

that way they dont need to look all the same.
WWWWWWWWWW

RomanClock

Looking into AS3 sound, its a bit different and uses classes instead of identifiers.
lemayo lol :soups:

RomanClock

lemayo lol :soups:

RomanClock

Guys, post some questions and ideas! I know I haven't finished this yet but I'd like to know if people are able to understand this.

Note: Looking into AS3 sound, its a bit different and uses classes instead of identifiers.

Additional Content: How to load an external MP3
lemayo lol :soups:

TorchClock

bump because i want to know how to make a vol slider


TorchClock


TorchClock


RomanClock

Ill get some updates after Clockday
lemayo lol :soups:

RomanClock

Completed position slider tutorial. Probably confusing so please ask questions for clarification.
lemayo lol :soups:

INoodleClock

There's a timeline bar, what about a bar for volume sir?

RomanClock

So I finally added the volume slider! Yay!
lemayo lol :soups: