You are on page 1of 10

http://www.actionscript.org/forums/showthread.php3?

t=32063

holder1 = createEmptyMovieClip("holder1", 1)
var m1 = new Sound(holder1)
m1.attachSound("musica1")
m1.start(0, 999)
m1.setVolume(100)
holder2 = createEmptyMovieClip("holder2", 2)
var m2 = new Sound(holder2)
m2.attachSound("musica2")
m2.start(0, 999)
m2.setVolume(0)

//Inicio del archivo de audio


stop();
var i:Number = 1;
//contador del mp3
var v:Number = 40;
//variable para medir el volumen del audio
var mp3s:Number = 10;
//Maximo de mp3 a reproducir
_root.miMp3 = new Sound();
_root.t1 = new Sound();
//variable principal mi mp3
_root.miMp3.loadSound("mp3/"+i+".mp3", true);
_root.t1.loadSound("mp3/t1.mp3", true);
//hacemos la carga de mp3 segun el valor de i
_root.miMp3.setVolume(v);
//asigna el volumen de reproduccion
_root.miMp3.onSoundComplete = function() {
//cuando termine el mp3, cargamos el siguiente
i++;
if (i>mp3s) {
i = 1;
}
miMp3.loadSound("mp3/"+i+".mp3", true);
_root.vol.gotoAndStop(v)
};
miMp3.onLoad = function(success:Boolean) {
var totalSeconds:Number = this.position/1000;
//vemos el tiempo transcurrido y sacamos los segundos
var minutes:Number = Math.floor(totalSeconds/60);
//lo mismo y sacamos los minutos
var seconds = Math.floor(totalSeconds)%60;
if (seconds<10) {
seconds = "0"+seconds;
}
if (minutes < 10) {
time_txt.text = ("0"+minutes+":"+seconds);
} else if (minutes >=10) {
time_txt.text = (minutes+":"+seconds);
}
};
setInterval(miMp3, "onLoad", 1000);
// lo que viene es para el porcentaje cargado
onEnterFrame = function () {
porcentaje_txt.text = (Math.round((miMp3.getBytesLoaded()/miMp3.getBytesTotal())
*100)+"%");
if (Math.round((miMp3.getBytesLoaded()/miMp3.getBytesTotal())*100) == 100) {
porcentaje_txt.text = "100%";
}
};
miMp3.onID3 = function():Void {
ide_txt.text = miMp3.id3.artist+" - "+miMp3.id3.songname;
t1.text = t1.id3.artist+" - "+t1.id3.songname;
};
//---------------------------------------
_root.vol_txt.text = "volumen a "+v+"%";
//Volumen inicial -----------------------
//funciones para el boton stop
_root.stop_btn.onPress = function() {
miMp3.stop();
miMp3.setVolume(v);
};
//funciones para el boton play
_root.play_btn.onPress = function() {
miMp3.start();
miMp3.setVolume(v);
};
//funciones para el el boton anterior
_root.prev_btn.onPress = function() {
if (i>1) {
//Condicion que asigna que si es mayor a 1
i = i-1;
//entonces sera igual a i restando 1
miMp3.loadSound("mp3/"+i+".mp3", true);
//cargara el valor de i
miMp3.setVolume(v);
//asigna el valor global del volumen
} else if (i <=9) {
//segunda condional que dice que si i es menor o igual a 3
i = 9;
//borre el valor anterior y asigne 3
miMp3.loadSound("mp3/"+i+".mp3", true);
//carga un mp3 con el valor de i
miMp3.setVolume(v);
//asigna el volumen global al archivo
}
/*En el anterior bloque de condiciones, creamos un bucle de forma manual, donde
si i llega
a un valor menor a 1, si presionamos el boton seguira restando en 1, entonces cr
eamos una
segunda condicion en la misma estructura donde si i no es menor o igual a 3, bor
re el valor
previo y re asigne a 3; con lo que obliga al programa a cumplir la primer condic
ion, creando
un bucle infinito en esta ecuacion.*/
};
//funciones para el boton siguiente
_root.sigu_btn.onPress = function() {
if (i<9) {
i = i+1;
miMp3.loadSound("mp3/"+i+".mp3", true);
miMp3.setVolume(v);
} else if (i >=9) {
i = 1;
miMp3.loadSound("mp3/"+i+".mp3", true);
miMp3.setVolume(v);
}
/*Hacemos lo mismo que en el boton anterior solo que a la inversa, aqui agregand
o valores en 1
en lugar de restarlos */
};
//funcion para el boton bajar volumen
_root.volMenos_btn.onPress = function() {
if (v>0) {
//creamos una condicion que dicte que si v es mayor a 0
v = v-5;
//el valor de v, sera v menos 5
miMp3.setVolume(v);
//asigna el volumen de la variable miMp3
_root.vol.gotoAndStop(v);
//lleva la barra de volumen al fotograma que dicte v
//en este caso se correra hasta 40; pues el valor original acertado
_root.vol_txt.text = "volumen a "+v+"%";
}
};
//funciones para el boton subir volumen
_root.volMas_btn.onPress = function() {
if (v<100) {
v = v+5;
miMp3.setVolume(v);
_root.vol.gotoAndStop(v);
_root.vol_txt.text = "volumen a "+v+"%";
}
};
//Fin del reproductor
/*MP3 Player by:
Sapinto: http://www.gallonegro.cl & Kitsch: http://javiernavarro.be | http://kit
schmultimedia.info
*/

Actionscript:
Sound.prototype.fadeVolume = function(level, duration) {
var me = this;
if (duration == undefined) {
duration = 1000;
}
clearInterval(me.intervalID);
me.intervalID = setInterval(function () {
me.vol = me.getVolume();
if (level-me.vol>0) {
me.inc = 1;
} else {
me.inc = -1;
}
if (Math.abs(me.vol-level)>0) {
me.setVolume(me.vol+me.inc);
} else {
clearInterval(me.intervalID);
}
}, duration/100);
};

* Home
* Tutorials
* Flash
* Intermediate
* Dynamic Sound Fade
Dynamic Sound Fade
* Email to Friend
* Print Article
* Remove from Favorites
* Add to Favorites
* Remove from 'Articles to Read'
* Add to 'Articles to Read'
By Muhammad Yahya
|
Published December 31, 1969
|
Intermediate
|
Rating: ratingfullratingfullratingfullratingfullratingempty Unrated
Page 1 of 1
Muhammad Yahya
This user is yet to take control of their account and provide a biography. If yo
u are the author of this article, please contact us via support AT actionscript
DOT org.
View all articles by Muhammad Yahya
Tutorial details:
Written by: Muhammad Yahya www.myahya.com webmaster@myahya.com
Difficulty Level: intermediate
Requirements: Flash MX
Adding sound to a flash movie is useful when you want your visitor not to be bor
ed by just surfing the complex 'action scripted' interface of your site. But add
ing sound is not easy. Just to import a sound into your library and then startin
g it with AS it not enough. You visitor can easily be annoyed if you don't give
him the access to control the sound. I have already added a link to the marvelou
s sound tutorial by Kenny Bellewin my flash tutorials section. But his state-of-
the art tutorial was even unable to fulfill my own needs. So I searched the net
for a solution and figured out how to give dynamic effects to my background soun
d. On this site you can clearly hear how the background sound fades in on start.
And then you have a button on bottom of the menu to stop it and to start it onc
e again. While stopping... the sound fades out dynamically. When you play it aga
in... it fades in. And the best part is that no matter how fast you keep clickin
g the toggle button... the sound works perfectly dynamically... fading in and ou
t.
I have to thank all the folks who helped me on the macromedia online web forums
and also on actionscript.org forums. Thumbs up for them. I really believe that a
s far as folks like them are around... people like me who wanna learn can't go d
isappointed.
Before getting started with the tutorial and telling you all the secrets to my v
ery own web site dynamism... I assume that you are not a novice in flash and kno
w how to work with action script in xpert mode. If you have just started learnin
g flash... or have worked in it but don't have much xperience with AS... I sugge
st you go to www.actionscript.org and do some of their basic tutorials... also d
o Kenny's sound tutorial linked in the tutorials section on this very site and t
hen return back... as you are alwayz welcome!!!
Please note that all the AS used in this tutorial has no copy rights. Feel free
to use it where ever you want. And if you think this tutorial was good enough, d
o link to it. The tutorial, its layout and format, however, is copyright yahya 3
d. Also note that this tutorial is for flash mx 6.0, it may not work properly in
flash mx 2004 7.0. Lets get started with the tutorial.
1.
First of all... let me tell you that this site has two scenes in it. The fist sc
ene contains the preloader and the second one contains the home page. So create
a new file in flash... and add a scene. This is the first step. We will work in
the second scene from here on.
2.
Now, obviously enough, you need to import a sound into your library. For that pr
ess ctrl+R and select your desired sound file.
3.
After doing so, open your library by pressing F11, locate the sound file, right-
click it and select 'Linkage'. Give it a name. In this tutorial we will use the
name 'backsound'
4.
Now that your sound file is exported for AS, lets get going with the magic. Goto
the first frame, open the action script panel and initiate the sound by writing
the following script:
// initiate sound
music = new Sound();
music.attachSound("backsound");
music.start(0, 999999);
5.
We don't want the sound to start abruptly, instead, we want it to fade in slowly
. For that, first of all we need to set the volume of the sound to zero.
// set the volume of the sound to zero
music.setVolume(0);
6.
Now we have to create a function that fades in the sound at the start. For the f
unction to work we need to set a couple of variables. You need to have knowledge
of them both to understand how it works. Our function for initial fade in goes
like this:
// set a variable named 'vol'
vol = 0;
// set another variable named 'fade', putting a setinterval function in it
fade = setinterval(fadeIn, 100);
// set the initial fade in function
function fadeIn() {
// fade the sound in with an increment of 3 in the variable 'vol'
vol += 3;
music.setVolume(vol);
// put an if condition to restrict the increment in volume after it reac
hes to 100
if (vol>=100) {
clearInterval(fade);
}
}
7.
What happens here is that this function fades the sound in dynamically with an i
ncrease of three to the volume which is set to zero. Please note that the increm
ent can be modified accordingly. When the volume reaches to hundered, the interv
al is cleared. Now we have to create another function... so that our sound can f
ade in and out according to the even and odd clicks on a single button. So we wi
ll write something like this in the AS window:
// function executed on onEnterFrame
_root.onEnterFrame = function() {
// set fade out
if (Fade == 1) {
vol = vol-step;
if (vol<0) {
vol = 0;
}
music.setVolume(vol);
// set fade in
} else {
vol = vol+step;
if (vol>100) {
vol = 100;
}
music.setVolume(vol);
}
};
8.
But there surely is something missing. You can clearly see that the key to this
fade in and out function is the use of two variables 'fade' and 'step'. Step is
the value you want the sound to fade in and fade out with and 'fade' is the vari
able that tells the function either to fade the sound in on the very click or fa
de it out. For this we have to go back to our initial fade in function and add t
wo lines defining the variables. Finally, our first fucntion will look like this
:
// set a variable named 'vol'
vol = 0;
// set another variable named 'fade', putting a setinterval function in it
fade = setinterval(fadeIn, 100);
// set the initial fade in function
function fadeIn() {
// fade the sound in with an increment of 3 in the variable 'vol'
vol += 3;
music.setVolume(vol);
// put an if condition to restrict the increment in volume after it reac
hes to 100
if (vol>=100) {
clearInterval(fade);
// create the 'step' variable
step = 1;
// create the 'fade' variable
Fade = 0;
}
}
9.
For the record... our complete script on the first frame will look something lik
e this:
// initiate sound
music = new Sound();
music.attachSound("backsound");
music.start(0, 999999);
// set the volume of the sound to zero
music.setVolume(0);
// set a variable named 'vol'
vol = 0;
// set another variable named 'fade', putting a setinterval function in it
fade = setinterval(fadeIn, 100);
// set the initial fade in function
function fadeIn() {
// fade the sound in with an increment of 3 in the variable 'vol'
vol += 3;
music.setVolume(vol);
// put an if condition to restrict the increment in volume after it reac
hes to 100
if (vol>=100) {
clearInterval(fade);
// create the 'step' variable
step = 1;
// create the 'fade' variable
Fade = 0;
}
}
// create the fade in and out function
// function executed on onEnterFrame
_root.onEnterFrame = function() {
// set fade out
if (Fade == 1) {
vol = vol-step;
if (vol<0) {
vol = 0;
}
music.setVolume(vol);
// set fade in
} else {
vol = vol+step;
if (vol>100) {
vol = 100;
}
music.setVolume(vol);
}
};
10.
Now, finally, create a button and add this script on it:
on (release) {
(_root.fade=!_root.fade) ? 0 : 1;
}
What happens is that when you click the button for the first time... it sets the
value of the variable 'fade' to 1, as the value of the variable 'fade' was set
to 0 in the first function, thus fading the sound out as the block with vol-step
executes. When you click the button for the second time it works vice versa. Th
e ? 0:1 command simply sets the value of the variable 'fade' to 1 or 0, dependin
g on what its previous value was.
Abra Cadabra!!! There you go... test your movie... should work perfectly fine.
var mySound2:Sound = new Sound();
//mySound2.loadSound("videos/opcion_1.mp3",true);
mySound2.attachSound("sonido_1");
mySound2.start(0,2);
mySound2.setVolume(0);
vol2 = 0;
trace(vol2);
step2 = 1;
Fade2 = 0;
fade2 = setInterval(fadeIn2, 100);
function fadeIn2() {
// fade the sound in with an increment of 3 in the variable 'vol'
vol2 += 0.5;
trace(vol2);
mySound2.setVolume(vol2);
// put an if condition to restrict the increment in volume after it reac
hes to 100
if (vol2>=100) {
clearInterval(fade2);
}
}

_root.onEnterFrame = function() {
if (Fade2 == 1) {
vol2 = vol2-step2;
if (vol2<0) {
vol2 = 0;
}
mySound2.setVolume(vol2);
// set fade in
} else {
vol2 = vol2 + step2;
if (vol2>100) {
vol2 = 100;
}
mySound2.setVolume(vol2);
}
};
var mySound:Sound = new Sound();
//mySound.loadSound("videos/opcion_1.mp3",true);
mySound.attachSound("casa_verde_1");
mySound.start(0,2);
mySound.setVolume(0);
vol = 0;
trace(vol);
step = 1;
Fade = 0;
fade = setInterval(fadeIn, 100);
function fadeIn() {
// fade the sound in with an increment of 3 in the variable 'vol'
vol += 0.5;
trace(vol);
mySound.setVolume(vol);
// put an if condition to restrict the increment in volume after it reac
hes to 100
if (vol>=100) {
clearInterval(fade);
}
}

_root.onEnterFrame = function() {
if (Fade == 1) {
vol = vol-step;
if (vol<0) {
vol = 0;
}
mySound.setVolume(vol);
// set fade in
} else {
vol = vol + step;
if (vol>100) {
vol = 100;
}
mySound.setVolume(vol);
}
};

You might also like