Link to home
Start Free TrialLog in
Avatar of CAE5942
CAE5942

asked on

End of audio detection

Hi everyone,

I have a flash file that has some sound that's being loaded via actionscript - see the full code in code view below.

The following function fades the movie clip but I need it to be triggered once the sound has finished playing through once:

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

I wondered if someone could tell me how I can detect whether the sound has finished playing so the function can run and the movie clip will fade out?

Appreciate any help.
import flash.events.Event;
import flash.events.MouseEvent;
import flash.net.URLRequest;
import flash.media.Sound;
import flash.media.SoundChannel;

var req:URLRequest = new URLRequest("sound.mp3");
var sound:Sound = new Sound();

function soundLoaded(event:Event):void
{
	sound.play();
}

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

background_mc.alpha = .2;

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


function fadeAdvert(e:Event):void
{	
	//Lowers alpha of advert movie clip - normal value is 1.
	advert.alpha -= .05;
	//Use this to fade your other MC.
	background_mc.alpha += .05; 
	//Checks to see if advert is faded out.
	if(advert.alpha <= 0)
	{
		advert.stop();
        		//If so, stop listening for the event, remove the advert, make ready for collection with the "null".
		stage.removeEventListener(Event.ENTER_FRAME, fadeAdvert);
		trace("faded and gone");

		skip_btn.visible = false;
	}
}

skip_btn.addEventListener (MouseEvent.CLICK, advertEndPurpose);
function advertEndPurpose(event:MouseEvent):void
{
		trace("FLV has ended");
		stage.addEventListener(Event.ENTER_FRAME, fadeAdvert);
		skip_btn.visible = false;
		advert.title_mc.visible = false;
}

Open in new window

Avatar of deepanjandas
deepanjandas
Flag of India image

var channel:SoundChannel = sound.play();
channel.addEventListener( Event.SOUND_COMPLETE, onSoundCompleteHandler );

Warm Regards
Deepanjan Das
Avatar of CAE5942
CAE5942

ASKER

Thanks for the reply,

After adding your code and testing, it seems to be working but I wondered if you could clarify a couple of things?

My original code included the following function which triggered the sound to play:

function soundLoaded(event:Event):void
{
      sound.play();
}

Your additional code included this line:

var channel:SoundChannel = sound.play();

Both of the above code snippets include the sound.play() function and I wondered if they're both needed for the application to work or am I doubling up on the code?

My second question is I have the following code with causes the fadeAdvert function to be triggered when the skip_btn button is pressed:


skip_btn.addEventListener (MouseEvent.CLICK, advertEndPurpose);
function advertEndPurpose(event:MouseEvent):void
{
            trace("FLV has ended");
            stage.addEventListener(Event.ENTER_FRAME, fadeAdvert);
            skip_btn.visible = false;
            advert.title_mc.visible = false;
}

This works as expected, however it doesn't stop the sound from playing. Can you tell what I'd need to add to the function in order to stop the sound?

Appreciate any further help.
Avatar of CAE5942

ASKER

I just need to know how to stop the sound when this code is run:

skip_btn.addEventListener (MouseEvent.CLICK, advertEndPurpose);
function advertEndPurpose(event:MouseEvent):void
{
            trace("FLV has ended");
            stage.addEventListener(Event.ENTER_FRAME, fadeAdvert);
            skip_btn.visible = false;
            advert.title_mc.visible = false;
}
to stop the sound, use channel.stop();

With AS3.0 code base, SoundChannel controls the sound stream, so we created an instance of the channel and contolled it with the help of the channel instance.

Warm Regards
Deepanjan Das
Avatar of CAE5942

ASKER

I added channel.stop(); into the following function:

function advertEndPurpose(event:MouseEvent):void
{
            trace("FLV has ended");
            stage.addEventListener(Event.ENTER_FRAME, fadeAdvert);
            channel.stop();
            skip_btn.visible = false;
            advert.title_mc.visible = false;
}

And then the function was called from this line of code:

skip_btn.addEventListener (MouseEvent.CLICK, advertEndPurpose);

When I test it though, the sound doesn't stop.

Do you know what I'm doing wrong?
Is the scope of channel instance available in the function?
Can you please share the whole snippet where you are playing the sound stuff along with the stop call.

There are patches to stoop all sounds in flash using SoundMixer but would not like to do that as channel.stop() should do it if all is good code.

Please share ur code stuff.

Warm Regards
Deepanjan Das
Avatar of CAE5942

ASKER

I'm not sure about the scope.

I've included the full code below. Can you take a look?
import flash.events.Event;
import flash.events.MouseEvent;
import flash.net.URLRequest;
import flash.media.Sound;
import flash.media.SoundChannel;

var req:URLRequest = new URLRequest("sound.mp3");
var sound:Sound = new Sound();

function soundLoaded(event:Event):void
{
	sound.play();
}

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

var channel:SoundChannel = sound.play();
channel.addEventListener( Event.SOUND_COMPLETE, advertEnd );

background_mc.alpha = .2;

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


function fadeAdvert(e:Event):void
{	
	//Lowers alpha of advert movie clip - normal value is 1.
	advert.alpha -= .05;
	//Use this to fade your other MC.
	background_mc.alpha += .05; 
	//Checks to see if advert is faded out.
	if(advert.alpha <= 0)
	{
		advert.stop();
        		//If so, stop listening for the event, remove the advert, make ready for collection with the "null".
		stage.removeEventListener(Event.ENTER_FRAME, fadeAdvert);
		trace("faded and gone");
		background_mc.uniformsLink_btn.visible = true;
		background_mc.RetailLink_btn.visible = true;
		skip_btn.visible = false;
		advert.title_mc.visible = false;
	}
}

skip_btn.addEventListener (MouseEvent.CLICK, advertEndPurpose);
function advertEndPurpose(event:MouseEvent):void
{
		trace("FLV has ended");
		stage.addEventListener(Event.ENTER_FRAME, fadeAdvert);
		channel.stop();
		skip_btn.visible = false;
		advert.title_mc.visible = false;
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of deepanjandas
deepanjandas
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of CAE5942

ASKER

Thanks so much - that's perfect.

Just another general comment: Since I've used actionscript to load the sound file, there's really no way I can synch animation to the sound in the flash authoring environment is there? I mean if I had added the sound to the timeline without actionscript, I could use the timeline to try and figure out where to place each of the movieclip animations, but as I've inserted the .mp3 file programatically there's no way to sync up an animation accurately with the sound is there?
I guess no, there will be lack of accuracy in that. Or you can split the mp3 into multiple files and play each from within the movieclips to match it, though might not be 100% accurate.

Best it to have it on the timeline, I guess.

Warm Regards
Deepanjan Das
Avatar of CAE5942

ASKER

Ok great - thanks again for all your help.