Solved

Actionscript to fade out a looped sound in a movie clip

Posted on 2004-09-22
17
2,985 Views
Last Modified: 2008-01-09
I have a sound event called "bikeRiderMusic" on frame 20 of a 35-frame movie clip called "bikeRider".    I want it to fade out after about 15 seconds, stay muted for 3-5 seconds, then fade back in ... etc., etc.  

Also I would like to have a button on the main timeline which enables the user to mute that audio only.

Thank you

Gabriel
0
Comment
Question by:gabrielPennyback
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
  • 3
17 Comments
 
LVL 7

Accepted Solution

by:
fixitben earned 175 total points
ID: 12126460
Check out this answer that I did a little while ago http://experts-exchange.com/Web/WebDevSoftware/Flash/Q_21121789.html 

Then For the mute button just Create a Movie clip with two frames in one frame have this

globalsound = new Sound();
globalsound.setVolume(100);
stop();

Then in the other have

globalsound = new Sound();
globalsound.setVolume(0);
stop();

Then just have a button and add this to it

on(release)
{_root.McWith2Frames.nextFrame();}

Also I have a Volume slider if you would like to use that all you have to do is drop it into the fla you are working on.  

Hope This Helps
Fixitben
0
 
LVL 1

Author Comment

by:gabrielPennyback
ID: 12127314
Excellent!  Thank you.

With regard to the first half (fading out):  Is there a way to do this with action script on frame 1 of my 1-frame main timeline?  Since I accidentally created this animation at 24fps, I would need 480 frames to complete the full 20-second cycle, which would be okay except that I'm trying to be more lean and streamlined in my flash files.

Thanks again, and yes I would love your volume Slider!  How do I get it?

Gabriel
0
 
LVL 4

Assisted Solution

by:mrdilliard
mrdilliard earned 175 total points
ID: 12128138
I would make a function to do this:

function fadeIn(){
   if(soundFile.getVolume() < 100){
      soundFile.setVolume(soundFile.getVolume()+1);
   }else{
      clearInterval(fadeIt);
   }
}

function fadeOut(){
   if(soundFile.getVolume() > 100){
      soundFile.setVolume(soundFile.getVolume()-1);
   }else{
      clearInterval(fadeIt);
   }
}

Then just use:
var fadeIt = setInterval(fadeIn(),100);
or
var fadeIt = setInterval(fadeOut(),100);

100 is the number of milliseconds between when the function runs. With these settings, they'll run 10 times per second (1000/100), meaning volume will take 10 seconds to completely fade out or in. If you want it to take about 5 seconds, take that number down to about 50.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:gabrielPennyback
ID: 12128330
To:  mrdilliard
from: gabrielPennyback

This is great! but I'm not sure what I need to do in terms of instance names, where to place the action script (what frame of what timeline?), etc.  

I tried replacing every instance of "soundFile" with the name of my sound clip as it appears in the library, but that didn't have any effect.

I definitely need more specific guidance here.

Thank you,

gabriel



0
 
LVL 4

Expert Comment

by:mrdilliard
ID: 12128402
Sorry I wasn't more specific.

You can place the functions anywhere on the timeline, as long as they're placed before you actually call them somewhere.

soundFile is going to refer to a new sound that you create using the library sound.

The syntax goes like:

var soundFile = new Sound()
soundFile.attachSound("name of file in library")
soundFile.start()

You can get more info at http://www.macromedia.com/support/flash/action_scripts/actionscript_dictionary/actionscript_dictionary654.html

By the way, you could do what I'm doing in an onEnterFrame event, but setInterval's a little cleaner to use, since it runs independently of the clips.
0
 
LVL 7

Expert Comment

by:fixitben
ID: 12128475
Yup that is what You gotta do.  I was just giving you an easier but more labor intensive way.  

Fixitben
0
 
LVL 7

Expert Comment

by:fixitben
ID: 12128596
Oh yeah and here is the file for the slider just copy and paste the slider to yours and it works http://theyac.org/experts/slider.fla 

Fixitben
0
 
LVL 1

Author Comment

by:gabrielPennyback
ID: 12129089
I think I keep hitting submit by accident before I've finished the comment.  Sorry about that!

Once again,

to fixitben:  what else do I have to do in my .fla in order for the volume Slider to affect the volume of my audio clip called "loop1"?  If I place it as is, nothing happens.

To mrdilliard:

After defining the two functions for fading in and out on frame 1 of my MC, do I then physically place my sound on frame 36 where I want it to begin?

Or do I just insert this script: ?

var soundFile = new Sound()
soundFile.attachSound("name of file in library")
soundFile.start()

Or both?  And where do I place the script?

Finally, where do I place these scripts:

var fadeIt = setInterval(fadeIn(),100);
or
var fadeIt = setInterval(fadeOut(),100);

Sorry for being so dense.  Thank you for all your help.

gabriel




0
 
LVL 7

Expert Comment

by:fixitben
ID: 12129172
Did you copy it from the library or the stage you need to copy it from the stage.  If  you did that then change this

onClipEvent (load) {
globalsound = new Sound();
}
onClipEvent (enterFrame) {
      globalsound.setVolume(_root.volume*2);
}

to

onClipEvent (enterFrame) {
      soundFile.setVolume(_root.volume*2);
}
0
 
LVL 1

Author Comment

by:gabrielPennyback
ID: 12129286
I tried copying from both the stage and the library.

The only script I see on the slider.fla that I copied is this:

this.ratio = 0;
_root.volume = 100;
dragger._x = 100;
dragger.onPress = function() {
      this.startDrag(true, 0, 0, line._width, 0);
      this.onEnterFrame = function() {
            ratio = Math.round(this._x*100/line._width);
            _root.volume = ratio;
      };
};
dragger.onRelease = dragger.onreleaseOutside=stopDrag;

that's it!  Where do I go to find this other bit of script to modify?
0
 
LVL 7

Expert Comment

by:fixitben
ID: 12129379
just click on the slider on the main stage once don't go inside it  The code you posted above is inside the MC slider.  If that isn't there then it won't work that is added  to the Slider Mc look at the File that I sent you and you will see where it is.  

Fixitben
0
 
LVL 1

Author Comment

by:gabrielPennyback
ID: 12129759
To Fixitben:  Well I must have never actually copied it from the stage before ... But that did it!!  Thanks!

I still can't find that other bit of script anywhere.  For future reference, where exactly would you place that script ...

Also, mr dilliard's all-actionScript solution looks great, I'd like to start using stuff like that, but I can't figure out how to make it work.  For additional points, can you walk me through the steps with extreme specificity?  I always seem to miss something that no tutorial will ever tell you, such as your hint that I needed to copy the slider from the stage!

Thanks again,

Also, I want to accept your answer so you can get the points.  But I'm new to Experts Exchange, and I don't how to keep in communication with you once I close the question.  Please advise.

0
 
LVL 7

Expert Comment

by:fixitben
ID: 12129821
OK on the Main Stage Just Click on the Slider Once Then look at the AS box at the bottom it will be there.

And The thing is I tried his code And couldn't get it to work either Everything looks right But I just can't find the problem.  What I would do is wait a couple of days before you give the points away because he will probaly post again.   Then if he doesn't anwser you back then Just either Split the points by clicking that button on your page or just accept one answer by clicking the accept answer button.  Also about communication afiter the question is closed  Just Post something on here Like hey fixitben got a question about this slider and it will still send me a email even after the question is closed.  Then I will check it and see what is up.  So Basically a Question is never closed just the points are awarded to a certian person.

Hope this Helps
Fixitben
0
 
LVL 4

Expert Comment

by:mrdilliard
ID: 12130326
Yes, I'll certainly post...

I had a logic problem with my last set of functions...I was checking to see if the volume was greater than 100...yeah....

Anyway, here's some code you could slap into the first frame of your movie, and just replace instance names, as necessary.

function fadeIn() {
      if (soundFile.getVolume()<100) {
            soundFile.setVolume(soundFile.getVolume()+1);
      } else {
            clearInterval(app.fadeIt);
      }
}
function fadeOut() {
      if (soundFile.getVolume()>0) {
            soundFile.setVolume(soundFile.getVolume()-1);
      } else {
            clearInterval(app.fadeIt);
      }
}
mcButton.onRelease = function() {
      clearInterval(app.fadeIt);
      if (this.playing || this.playing == undefined) {
            this.txtDisplay.text = "FADE IN";
            this.playing = false;
            app.fadeIt = setInterval(fadeOut, 10);
      } else {
            this.txtDisplay.text = "FADE OUT";
            this.playing = true;
            clearInterval(fadeIt);
            app.fadeIt = setInterval(fadeIn, 10);
      }
};
_global.app = this;
//reference to _root
var soundFile= new Sound();
//located in library with linkage set to "mySound"
soundFile.attachSound("mySound");
soundFile.start(0, 999999);


Also, you can get the fla at my site if you'd prefer that. I even put in a crappy techno beat as my sound.

http://www.squirreltweezers.com/development/sound_file.fla (231K)
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SWF capture the time on the Video 3 240
Where to download Acrobat flash offline installer? 2 269
High definition conversion videos in YouTube 12 301
YouTube video issue 28 142
This article describes a solution to a problem of subloading one movie into another when they have different SWF versions. Sometime back, I was working on an ActionScript project while I came across an interesting fact which I would like to share…
While working over numerous projects I often had the requirement for doing a screen capture in AS3.0. Unfortunately I found no "ready made" solutions in google search that suited my requirements. But I did come across some great resources which help…
The goal of the tutorial is to teach the user how to how to load their YouTube profile onto Flash Media Live Encoder.
The goal of the tutorial is to teach the user how to use the auto adjust feature and what the different options do. When your video is not working right you can choose the auto adjust feature to help choose your settings.

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question