Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Thank you so much for your fast and widespread response.
I add the code and try some combinations. But it's only getting worse.
I know nothing about flash, that's why I use the youtube movie. My play/stop button is complete, only the play button must show up again after the sound finishes.
Is it possible for you to add a code to help me in flash? When I see your comment, you must be a professional in flash.
I can send you the Fla.!
You can send me an e-mail to contact me on [Removed personal information], so I can mail you back.
Message was edited by: sinious
Copy link to clipboard
Copied
You shouldn't post personal contact information here, spiders will eat it up so I removed it.
Being you're so new to the product I'll suggest an easier route. Here's a different tutorial that goes over what I essentially just explained, by the same author of your previous video:
http://www.youtube.com/watch?v=QZAnlMaSxCQ
I didn't watch the entire movie but I scrubbed it to read the code and I can see he's using code similar to mine. Give it a try and/or download his source.
Copy link to clipboard
Copied
Thank you for the warning to write no personal contact information and for removed it.
I know this tutorial and its better to remake my button. Its not take long so I gonna make it today!
I let you know when I did. And if the button suffice to my objective.
Copy link to clipboard
Copied
Thank U so much. I have finally the button that I want. It is a simple button for Flash professionals but for me it was hard.
Thanks again!!
Copy link to clipboard
Copied
Glad you got it working and if you're all set please mark a correct answer so we can filter unanswered questions.
You're welcome and good luck!
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more