Skip to main content
Participant
December 18, 2013
Question

Flash CS3 Play/Stop audio button problem

  • December 18, 2013
  • 1 reply
  • 2542 views

I made a play/stop audio button with this youtube tutorial (youtube.com/watch?v=XU6oMEFUFF8) but after the sound is finished I want the play button to show up agian.

Here is the actionscript:

import flash.events.MouseEvent;

play_btn.addEventListener(MouseEvent.CLICK,clickha ndler);
stop_btn.addEventListener(MouseEvent.CLICK,clickha ndler);
function clickhandler(event:MouseEvent):void{
swapChildren(play_btn,stop_btn);
}

what code should I add to my current code?

I also can send you the Fla.

This topic has been closed for replies.

1 reply

sinious
Legend
December 18, 2013

It's a good idea to include all the code in your question to get a better response.

Where is the code loading the sound?

Participant
December 18, 2013

By both buttons - play and stop I made a layer named "music". I removed the HIT keyframe and in the DOWN keyframe I drag a music-file from the library.

sinious
Legend
December 18, 2013

You should consider using the Sound class. Here's a quick overview of it:

http://help.adobe.com/en_US/as3/dev/WS5b3ccc516d4fbf351e63e3d118a9b90204-7d21.html

You'll be able to add event listeners to the sound to know when it completes so you can execute more code, such as swapping the play and stop button. There is an example in that overview of listening for the SOUND_COMPLETE event (e.g. channel.addEventListener(Event.SOUND_COMPLETE, onPlaybackComplete);). It should contain everything you need.

A basic example of what you'll end up with (there's many ways to do it) might look something like this:

// just set up and wait for click

var isPlaying:Boolean = false;

var mySound:Sound = new MySoundInLibrary();

var myChannel:SoundChannel = new SoundChannel();

myChannel.addEventListener(Event.SOUND_COMPLETE, onPlaybackComplete);

// buttons

play_btn.addEventListener(MouseEvent.CLICK,clickhandler);

stop_btn.addEventListener(MouseEvent.CLICK,clickhandler);

function clickhandler(event:MouseEvent):void

{

 

          swapChildren(play_btn, stop_btn);

 

          if (isPlaying)

          {

                    isPlaying = false;

                    myChannel.stop();

          }

          else

          {

                    isPlaying = true;

                    myChannel = mySound.play();

          }

}

function onPlaybackComplete(e:Event):void

{

          isPlaying = false;

          swapChildren(play_btn, stop_btn);

}

The basic idea is keeping a flag if the audio is currently playing. If it isn't then assign the sound to a channel and play it while setting the flag (isPlaying) to true. If it's already playing, set the flag to false and stop playing the audio.

The swapChildren is run in the onPlaybackComplete() method so when the audio completes the play button is swapped back in front.

Rather than swapping the children I'd urge you to toggle the .visible property instead. It's easy to run commands in the wrong order and get desynched on depth.