We help IT Professionals succeed at work.
Get Started

Checking to see if sound sound file has finished on timeline

314 Views
Last Modified: 2012-05-11
Hi everyone,

I'm using the actionscript code below to load a .mp3 file and then it checks to see when the sound is complete and then runs a function which fades another movieclip.

The code works as expected but as I need to synchronise another animation with the sound, I thought it would be easier to have the sound file on the timeline itself instead of loading it using actionscript. If I do it that way though, I still need to use actionscript to check when the sound has finished playing so that the "advertEnd" function can run.

Can someone tell me how I'd revise the code below so that it only checks to see if the sound has finished and runs the function?

Appreciate any advice.
var req:URLRequest = new URLRequest("sound.mp3");
var sound:Sound = new Sound();
var channel:SoundChannel;

function soundLoaded(event:Event):void
{
	channel = sound.play();
    if( !channel.hasEventListener( Event.SOUND_COMPLETE ) ){
            channel.addEventListener( Event.SOUND_COMPLETE, advertEnd );
       }
}

sound.addEventListener(Event.COMPLETE, soundLoaded);
sound.load(req);


function advertEnd(e:Event):void
	{
		trace("FLV has ended");
		stage.addEventListener(Event.ENTER_FRAME, fadeAdvert);
	}

Open in new window

Comment
Watch Question
Top Expert 2009
Commented:
This problem has been solved!
Unlock 3 Answers and 4 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE