View Full Version : control music fade out on external swf
dafnabru
01-19-2007, 10:07 AM
Im designing a website that has the option for 3 music loops as background music. On the site, I have 3 buttons (1, 2 and 3) which each load a swf with an mp3 on the first frame as streaming and with fade in (from the sound properties). A fourth button simply unloads the external swf.
Till now everything works fine, the problem is that my client wants the music to fade out when unloaded or when another one is loaded.
Is there a way I can control with a button on the main swf, that the music on the external swf will fade out and then unload?
thanks! I hope someone can help...
Mazoonist
01-19-2007, 02:58 PM
Hey, dafnabru,
In my opinion, the only thing that will give you complete control over sounds is to create a Sound object. Start working with the methods and properties of the Sound class.
Rather than loading an external SWF with a sound in it, you can create a Sound object and load an MP3 directly into it:
var fileName:String = "TheNameOfYourSongHere.mp3";
this.createEmptyMovieClip("holder", this.getNextHighestDepth());
var mySound:Sound = new Sound(holder);
mySound.loadSound(fileName, true);
mySound.start();
play_btn.onRelease = function() {
mySound.start(mySound.position / 1000)
}
stop_btn.onRelease = function() {
mySound.stop();
}
Here's another version of the same code with a fadeOut function called on a setInterval when the stop button is pressed:
var fileName:String = "TheNameOfYourSongHere.mp3";
this.createEmptyMovieClip("holder", this.getNextHighestDepth());
var mySound:Sound = new Sound(holder);
mySound.loadSound(fileName, true);
mySound.start();
play_btn.onRelease = function() {
mySound.setVolume(counter);
mySound.start(mySound.position / 1000)
}
stop_btn.onRelease = function() {
clearInterval(nInterval);
nInterval=setInterval(fadeOut, 50);
}
var counter:Number = 100;
function fadeOut() {
mySound.setVolume(counter);
if(counter < 1) {
clearInterval(nInterval);
mySound.stop();
counter = 100;
} else {
counter--;
}
}
I realize it's not super-sophisticated or anything, that's all I have time for right now (it's missing a fadeIn function, for example), but I think you'll probably be able to see that by playing with setVolume and some of the other methods and properties of the sound class, you can make it do just about anything you want. Hope that helps.
dafnabru
01-22-2007, 12:58 PM
Thank you so much!! In the meanstime its working fine. But im struggling making the sound fade in. Also, I have three different sound buttons, so I need that when the first one is playing and I press the second button, the first one fades out and the second one fades in. Could you give me a hint?
thank you!! you are saving my life!!
dafnabru
01-22-2007, 01:26 PM
hey! just one more little thing. is there a way to load the external mp3s in a streaming way? because it takes some time till they start playing. Their sizes are 325k, 248k and 126k.
Thanks!!:)
Mazoonist
01-22-2007, 03:17 PM
What I gave you already IS the streaming method. The second parameter of the loadSound method tells it whether to stream or not. Whatever lag there may be is just whatever time it takes for enough of the sound to load in order to fill the buffer and start playing. BTW, setting that parameter to false tells it not to stream the sound, in which case the whole thing has to load before it starts playing. This would be similar to what you were doing with the external SWF approach.
But, your sound files don't seem particularly huge. You should know that there is yet another way: attachSound (in AS, "attach" usually means bring in from the library, and "load" usually means bring in from an URL). In this version, you import the sound files to the library, right click each one, set the linkage to export and give it a linkage identifier name. This particular approach has the advantage that when you need your sounds, they're already loaded and ready. But it has the disadvantage that the overall SWF file is going to take longer to load, and if it's really significant, you might want to put a preloader on the whole thing. Now, I'm not saying to use this approach, you'll have to decide that for yourself. You should know that it's an option, though.
I've gotta go right now, but later on today I'll check back here, and try to give you some help on the fade(s). :)
dafnabru
01-22-2007, 10:51 PM
hhmm.... so what can I do? because in my computer it does take some time, and its specially anoying when I go from one sound to the other because it seems that there is no sound and after like 10 seconds the sound starts.
with my other "technique" in which i loaded an swf with the music in the first frame, I had it as streaming and it started just on time. I also had it as "fade in" in the sound properties. the only thing I had missing was the fade out part. beleive me, i would leave it like that, but the client im doing the site for wants the music to fade in and out.
maybe there is a way to tell the sound in the first frame to fade out? im sure there something like that... Ill keep looking around. or i will keep trying with "your technique"
thank you so much!! i really apreciate the help:)
Mazoonist
01-23-2007, 01:50 AM
On my computer, a streaming sound starts right away (and I'm using a 2MB music file to test with) . The only thing that slows it down is if I "simulate download" and have it set at 56K. That makes it take about 8 seconds and then it's all choppy. If you're testing locally, and you're not simulating the download, it should start right away for you. So I don't get it. Or are you testing it online? If that's the case, I understand, as you will probably want to see what it's like in real usage.
There is nothing about using Actionscript that makes it "my technique." Basically, you've got to load a sound file into a Sound object, then you can use Actionscript to tell it what to do. There are many useful functions and properties, but there is no "fadeOut" or "fadeIn" functions, so you have to write your own. There is a "setVolume" function, though. You can set the volume anywhere from 0 to 100. So it made sense to me to use setVolume to set the volume to a lower and lower value, over time, using setInterval for the timing. Then you get a fade out effect.
I think what you will want to do is start all the sounds loading before the button is pressed. In other words, don't use the button press as the trigger to load the sound. Use the button press as the trigger to start the sound.
It'd be kind of nice to see the file, I might be able to help you better.
dafnabru
01-23-2007, 09:01 AM
Im obviously tasting it online!! and you know whats weird? the first sound starts more or less on time, a couple of seconds after the loading of the website ends. but when i click the other two, it takes ages! maybe if i would put a preloader bar, it would be good.
im trying to load the second and third tracks from the first frame, but its not working. in the first frame i have:
var fileName:String = "1.mp3";
this.createEmptyMovieClip("holder", this.getNextHighestDepth());
var mySound1:Sound = new Sound(holder);
mySound1.loadSound(fileName, true);
mySound1.start();
var fileName:String = "2.mp3";
this.createEmptyMovieClip("holder2", this.getNextHighestDepth());
var mySound2:Sound = new Sound(holder);
mySound2.loadSound(fileName, false);
var fileName:String = "3.mp3";
this.createEmptyMovieClip("holder3", this.getNextHighestDepth());
var mySound3:Sound = new Sound(holder);
mySound3.loadSound(fileName, false);
but it doesnt make things better. i also copyed the fade out actionscript trying to make it fade in, but I dont know exactly how to use and where to put it. ive attached the sound movie clip that is on the first frame of the site. i havent included the mp3s cause they are too heavy. you can see the website at http://www.artlight.co.il/finaltest/artlight.htm but at the moment the sound loaded in swfs and not with actionscript. you can see a link to the sound test at http://www.artlight.co.il/finaltest/soundtest.htm
thanks!!!
Mazoonist
01-23-2007, 02:21 PM
Don't use the same variable name for the three files, and especially don't declare it three times with "var." The second and third occurances will overwrite the first. In fact, don't use the variable "fileName" at all, unless it's useful for you somehow. I only used a variable called "fileName" in my file so that I could change it easily in one place. It's certainly not required. So you need to do this:
this.createEmptyMovieClip("holder", this.getNextHighestDepth());
var mySound1:Sound = new Sound(holder);
mySound1.loadSound("1.mp3", true);
mySound1.start();
var fileName:String = "2.mp3";
this.createEmptyMovieClip("holder2", this.getNextHighestDepth());
var mySound2:Sound = new Sound(holder2);
mySound2.loadSound("2.mp3", false);
var fileName:String = ;
this.createEmptyMovieClip("holder3", this.getNextHighestDepth());
var mySound3:Sound = new Sound(holder3);
mySound3.loadSound("3.mp3", false);
Also, I corrected your use of the same "holder" three times. By using that same name, you're loading all three clips into the same timeline. That can cause a lot of problems. I will take a look at the file you've uploaded.
Mazoonist
01-23-2007, 02:38 PM
Inside your file, you are loading the sounds again and again. You only need to load them once. Then you just use the start() and stop() functions to control each sound. Think of "load" as being like "download." That's why you're getting a lag, you're downloading it all over again.
Mazoonist
01-23-2007, 02:57 PM
I reworked your file a bit. All those extra frames are not necessary. I gave instance names to all the buttons, and put all the code on frame 1 of an actions layer. You can easily control everything from there. All you have to do is substitute the names of your own sound files. When you press the buttons, the different sounds start and stop. Now the only thing you lack is the fade in and fade out! I'll work something up for you later on when I get home from work.
If you check out the code you'll see it's really quite simple to control sounds once they're loaded in.
dafnabru
01-23-2007, 08:33 PM
Thank you!! im at work too, so i will take a look at the files when i get home. I really apreciate your help;)
some fade in and out and my life is saved!!
thanks!
Mazoonist
01-24-2007, 03:03 PM
Hey, I hope you can bear with me for another day. The fade in & fade out thing is just a tiny bit more complicated than I at first thought. But I made great progress on it last night, and this morning I solved another part of it that had me stumped, and it's all but finished since the major problems are solved. I can't go into detail right now, but I should finish it tonight. Hopefully your deadline is not to pressing.
dafnabru
01-24-2007, 05:10 PM
:o wow! thank you!!! who would have thought it was so complicated...
thats how it is to work for someone, they think is nice to have some fades with the music and they think its just a matter of seconds... anyways thank you! cant wait to see the final product!! thanks!!
Mazoonist
01-25-2007, 03:00 PM
I just had a whole message composed here, and before I had a chance to post it, firefox said "I have to close. sorry for the inconvenience. send error report?" Arggh! I don't have time this morning to re-compose it. I'm just going to post the file and let it speak for itself for now. There are a couple of things you can customize: Whether a sound starts from the beginning each time, or picks up where it left off (right now, it picks up where it left off). Also the speed of the fades can be adjusted to your liking with the number you send to setInterval. I think you'll like the result. Have a great day!
dafnabru
01-26-2007, 01:50 PM
thank you soooo much!! i really apreciate your work, ive learnt a lot from seeing how you do the files. the script is excellent, and I will definitly use it!
thanks!!!!!!!!!!!!!!!!!!!!!!!!!! :)
Mazoonist
01-26-2007, 02:51 PM
Well, cool, glad I could help. I'm pretty much still intermediate as an actionscript programmer, so helping people on forums teaches me a lot, too. I'm going to try to reconstruct what I wrote yesterday (the message I lost!):
What made this task a bit more complicated is the fact that the user can press any sound button, and the program has to fade out whatever sound is currently playing (if any). So the program has to know what sound is playing at any given time, hence the "currentSound" variable. Then (even after writing the fadeOut and fadeIn functions), you can't just tell it to fade out the first sound and fade in the second one, or that'll happen all at once. You need to have some way to say fade out the first sound, and then when that's finished, fade in the second one. That's what had me stumped the first day. I solved it by making a custom "onFaded" event. Also, you don't necessarily want any button interaction while the transition is taking place. It seems easier to just not allow a button press until the fades are completed. That's the reason for the "busy" variable.
So you'll notice in the first line of each button's actions, if the currentSound is the same as the one associated with the button being pressed or "busy" is true, the action there is "return" (which means, leave this function, done). So if someone is clicking on the sound that's already playing, you want to ignore that button click somehow. Also, if busy is true, the program is currently in a transition between two sounds, and you want to ignore the click in that case too. The "return" command makes the request basically "bounce off" in either of those two cases.
If it makes it past that first condition, the remaining lines determine which sound it is that's currently playing, fades that one out, then fades in the one associated with that particular button. If no sound is playing (currentSound equals 0), someone must have previously pressed the stop button and stopped all the sounds. In that case, no fade out is requested, just a fade in on that button's sound.
If you want the sounds to always start from the beginning, go to the fadeIn function, and in the start() command, you'll see where it says:
theSound.start(theSound.position / 1000);
All you have to do is delete everything in the parentheses:
theSound.start();
I mentioned previously that you can control how fast the fading occurs. You can change the number given to setInterval. Right now it's set at 20. If you were to put 40 instead, you would get a fade that's twice as long. Setting it at 10 would make it twice as fast. There is another way, too. Inside the adjust volume functions, find where it says:
counter++;
and put something like:
counter += 3;
And that will make the volume increment 3 at a time instead of only one, making the fade three times faster. You can experiment. I tried making it fade out faster than it fades in, and it sounded pretty good that way.
Mazoonist
01-26-2007, 03:03 PM
You might also want to add these lines to the end of the script:
mySound1.onSoundComplete = function() {
mySound2.start();
}
mySound2.onSoundComplete = function() {
mySound3.start();
}
mySound3.onSoundComplete = function() {
mySound1.start();
}
Then, if no button interaction takes place, when one sound completes, the next one begins. There would be no fading in this case. I haven't tested the above, but I have a strong hunch it should work with no problems. Have a great day!
dafnabru
01-26-2007, 06:59 PM
cool, thanks for the explanation. now everything seems clearer!
its good to know that you like to practice actionscripting cause im not so good at it. i finished this website for the moment but there are more coming , so its good to know that you are here;)
thanks!!
|
vBulletin® v3.8.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.