Groups | Blog | Home
all groups > flash actionscript > february 2007 >

flash actionscript : Sound Fade onEnterFrame


David Stiller
2/19/2007 4:20:07 PM
Dave,

[quoted text, click to view]

If you want to fade over seconds, you might rather want to go with a
mechanism like setInterval(), which bases itself on milliseconds.
onEnterFrame is based on framerate, which ... well, which depends on your
framerate. ;) And framerates are arbitrary.

[quoted text, click to view]

So far, so good -- with the caveat that this function will occur at a
rate of time consistent with your framerate. This may or may not take three
seconds.

[quoted text, click to view]

Not sure why so is necessary, if you already have an audio reference.

[quoted text, click to view]

This can be simplified a bit (more in a moment).

[quoted text, click to view]

This isn't too far off. Here's an approach I might take.

// Assuming an audio variable already ...

// Rather than constantly checking audio.getVolume(),
// set an arbitrarily named vol variable to zero, since
// we're fading in ...

var vol:Number = 0;

this.onEnterFrame = function():Void {
if (vol < 100) {
vol += 5;
audio.setVolume(vol);
} else {
audio.setVolume(100);
delete this.onEnterFrame;
}
}


David Stiller
Adobe Community Expert
Dev blog, http://www.quip.net/blog/
"Luck is the residue of good design."

dmschenk
2/19/2007 6:50:55 PM
Please help!
I'm trying desparately to get the audio to fade over a 3 second period when
the movie gets to a specific frame near last few frames of my movie but haven't
been able to find any code that works for me. I've seen several that work for
button but haven't been able to convert the code for use in a frame based
execution.




Thanks
Dave

This is the code I started with:



this.onEnterFrame = function(){
so = audio;
rate = 5;
newVolume = so.getVolume() + rate;
if(newVolume < 100) {
so.setVolume(newVolume);
} else {
so.setVolume(100);
this.onEnterFrame = null;
}
}
David Stiller
2/19/2007 8:08:57 PM
dmschenk,

[quoted text, click to view]

Sure thing.

[quoted text, click to view]

Okay.

[quoted text, click to view]

Sure, that makes sense. :)

[quoted text, click to view]

It sure should. But I wonder if you simply invoked getVolume() on its
own, as if it were a function? Here's the thing about objects ... objects
are defined by something called classes. Classes determine a given object's
properties (characteristics), things it can do (methods), and things it can
react to (events). The Sound class sets apart sound objects from, say,
movie clip objects, because the Sound class defines different properties,
methods, and events from the MovieClip class.

By the time you're dealing with a given object, you have to invoke a
method *on* that object instance. e.g.

var vol:Number = audio.getVolume();

.... assuming, by that point, that audio is already a valid Sound instance.

Of course, maybe you already know all this, so I apologize if I'm
telling you thinks you already know. You should be able to trace() any
property or method at any time to kinda see what you're dealing with ...

trace(audio.getVolume());

.... which helps you see "under the hood," a bit.


David Stiller
Adobe Community Expert
Dev blog, http://www.quip.net/blog/
"Luck is the residue of good design."

David Stiller
2/19/2007 11:01:51 PM
Dave,

[quoted text, click to view]

If the volume does fade correctly, then I can only assume audio is a
valid Sound instance. That means somewhere along the line, you must have
written: var audio = new Sound(), or showhow set audio to an existing Sound
instance.

[quoted text, click to view]

That's one of many debugging tools native to Flash. See this article
for a bunch more. :)

http://www.quip.net/blog/2006/flash/debugging-actionscript


David Stiller
Adobe Community Expert
Dev blog, http://www.quip.net/blog/
"Luck is the residue of good design."

David Stiller
2/20/2007 12:00:00 AM
Dave,

[quoted text, click to view]

Aha! Okay. :) Then you're dealing with a custom SoundPlayer object,
and your audio variable is an instance of that object.

[quoted text, click to view]

Ah, good! Well, that means your SoundPlayer object -- that is, this
pre-built mp3 player -- has a currentPlay property, and this
currentPlayproperty is a Sound instance.

[quoted text, click to view]

Well, you do -- but your instance name refers to a SoundPlayer instance,
and whoever wrote this object chose not to provide it a getVolume() method.
But ... according to that line above, this SoundPlayer object has a
currentPlay property that *is* a Sound instance. So, it may be possible to
get to the volume like this:

trace(audio.currentPlay.getVolume());

See what's going on? audio is your SoundPlayer reference; SoundPlayer
has that currentPlay property, so audio.currentPlay is a valid reference.
Then, currentPlay refers to a Sound instance, and the Sound class features a
getVolume() method ... so the full path shown above should do it.


David Stiller
Adobe Community Expert
Dev blog, http://www.quip.net/blog/
"Luck is the residue of good design."

dmschenk
2/20/2007 12:11:28 AM
Thanks Dave
I played with the code you sent me and tweaked it a little bit and ended up
with this:

var vol:Number = 50;

this.onEnterFrame = function():Void {
if (vol > 1) {
vol -= 1;
audio.setVolume(vol);
} else {
audio.setVolume(0);
delete this.onEnterFrame;
}
}

I ended up setting the vol to 50 because that is where the volume is in the
movie. I tried using the getVolume(); to detemine the volume but that didn't
seem to work.

Anyway thanks for the help.
dmschenk
2/20/2007 3:29:52 AM
You were right I did try to use the getVolume by itself so I'll try what you
are showing me. I don't know that audio is a valid sound instance although the
volume does fade as it should.

I'm really new to actionscript so I appreciate all the tips... like the trace
statement! I do need to try that one more!!

Dave
Thanks for your help

Dave
dmschenk
2/20/2007 4:31:31 AM
Not quite.... I had added a pre-built mp3 player (SoundPlayer from flashloaded)
then gave the player an instance name of audio. I couldn't find anywhere in
its code where it set the instance name of the audio file other than a line of
code that said "this.currentPlay = new Sound(this);" so maybe me giving the
player itself was good enough for a portion of the code to work but not all of
it.

var vol:Number = audio.getVolume(); didn't work and neither did the
trace(audio.getVolume()); but I'm assuming that is because I don't really have
the correct instance name or path.

Dave

[quoted text, click to view]
dmschenk
2/20/2007 4:36:43 PM
Sweet!! That worked great!
So this was the final code configuation:

var vol:Number = audio.currentPlay.getVolume();
// trace(audio.currentPlay.getVolume());
this.onEnterFrame = function():Void {
if (vol > 1) {
vol -= 1;
audio.setVolume(vol);
} else {
audio.setVolume(0);
delete this.onEnterFrame;
}
}

Thanks for all the help!!!

Dave
dmschenk
2/20/2007 4:39:21 PM
Sweet!! That worked great!
So this was the final code configuation:

var vol:Number = audio.currentPlay.getVolume();
// trace(audio.currentPlay.getVolume());
this.onEnterFrame = function():Void {
if (vol > 1) {
vol -= 1;
audio.setVolume(vol);
} else {
audio.setVolume(0);
delete this.onEnterFrame;
}
}

Thanks for all the help!!!

Dave
AddThis Social Bookmark Button