[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

End of audio detection

Posted on 2011-05-10
11
Medium Priority
?
529 Views
Last Modified: 2012-05-11
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

0
Comment
Question by:gwh2
  • 6
  • 5
11 Comments
 
LVL 17

Expert Comment

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

Warm Regards
Deepanjan Das
0
 
LVL 1

Author Comment

by:gwh2
ID: 35728162
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.
0
 
LVL 1

Author Comment

by:gwh2
ID: 35728546
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;
}
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 17

Expert Comment

by:deepanjandas
ID: 35729979
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
0
 
LVL 1

Author Comment

by:gwh2
ID: 35734268
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?
0
 
LVL 17

Expert Comment

by:deepanjandas
ID: 35734775
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
0
 
LVL 1

Author Comment

by:gwh2
ID: 35735083
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

0
 
LVL 17

Accepted Solution

by:
deepanjandas earned 2000 total points
ID: 35735148
Here you go:
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();
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);



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);
                if( channel != null )
                    channel.stop();
                skip_btn.visible = false;
                advert.title_mc.visible = false;
}

Open in new window


Warm Regards
Deepanjan Das
0
 
LVL 1

Author Comment

by:gwh2
ID: 35735524
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?
0
 
LVL 17

Expert Comment

by:deepanjandas
ID: 35736039
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
0
 
LVL 1

Author Comment

by:gwh2
ID: 35736305
Ok great - thanks again for all your help.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article is primarily concerned with ActionScript 3 and generally specific to AVM2.  Most suggestions would apply to ActionScript 2 as well, and I've noted those tips that differ between AS2 and AS3. With the advent of ActionS…
I hope you'll find this tutorial useful and interesting. So let's try to extend Tcl with a new package.  For anyone more deeply interested please check out the book "Practical Programming in Tcl and Tk". It's really one of the best written books abo…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The goal of the tutorial is to teach the user how to select which audio input to use. Once you have an audio input plugged into the laptop or computer, you will go into the audio input settings and choose which audio input you want to use.
Suggested Courses
Course of the Month19 days, 14 hours left to enroll

872 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