View Full Version : Play music file once only on click of Play button until music stops.
Crocomire
May 1st, 2009, 02:03 PM
Hi there, new user here, looking for a solution to my problem. I'm still a beginner at Flash really so bear with me.
Using Macromedia Flash Professional 8.
I have a website banner made in Flash. I have added a music file to it and Play and Stop buttons. I also have a Replay button to replay the banner animation once it stops.
I have put the music on a separate frame so that it will not play automatically when the banner loads. I have made the Play button direct to the frame the music is located at so that when clicked, it will go to that frame and play the music file.
The problem I am currently having is that if I click the Play button more than once, it will play the music file again on top of the current play-through.
So if I click Play, the music will play. Then if I click it again, the music will play again, but on top of the first instance of the music, so two instances of it will now be playing and it sounds horrible.
I would like to make it so that if you click Play when the music is playing, it will not play the music again until the music has stopped.
Any solutions on how I can get over this? If you need any more information, I would be glad to provide it. Perhaps I should attach the flash file too to see if someone can do something with it? But I will wait for a reply first.
Thank you very much for any and all help in advance!
glosrfc
May 1st, 2009, 05:10 PM
play_btn.onPress = function() {
if (!playing) {
playing = true;
gotoAndPlay(framenumber);
}
};
glosrfc
May 1st, 2009, 05:14 PM
If you want the option to replay the sound when it's finished, insert sufficient frames to cover the length of the sound file, then insert a blank keyframe at the end and reset the playing variable to false:
playing = false;
Alternatively check the help files for how to set up a Sound Object - you can use the onSoundComplete handler to reset the variable when the sound has finished playing.
Crocomire
May 1st, 2009, 05:45 PM
I'm sorry, do I add this to the Play button?
Because when I did, I got the following error:
**Error** Scene=Scene 1, layer=Layer 9, frame=1:Line 1: Statement must appear within on handler
play_btn.onPress = function() {
glosrfc
May 1st, 2009, 06:30 PM
No, you add it to the timeline and give your button an instance name, e.g. play_btn. This ensures that your code is in one single location rather than being attached to a multitude of buttons and movieclips.
If you must attach it to the button, replace the first line with the appropriate handler, e.g. on (press) {
Crocomire
May 1st, 2009, 07:41 PM
Ah, I see now. I added it to the timeline and repeated clicks of the Play button don't do anything, they keep the original music playthrough going on its own, which is what I wanted =]
However, I'm having a little trouble still. When I press the Stop button, the music stops. But when I click Play again (after hitting Stop), the music does not play. Same happens if I hit the Replay button (which takes me to the first frame to replay the animation again), then hit Play - it doesn't play again.
I read what you said about adding sufficient frames to cover the length of the sound file, but my sound file is over 2 minutes long and even adding those frames to find the end and then doing what you said, I ended up having trouble again and hitting Play over and over kept repeating the sound file.
Currently, I have one frame containing the sound file, at the very end of my timeline.
It's not a huge deal at the moment. I think I can live with this problem of not having the Play button play the music again after it's stopped or the Stop/REplay button is pressed, but if there's any chance of perhaps a better explanation on how to overcome this, it would be great. I know it's probably annoying trying to get newbies like me to understand though!
Appreciate all your help though - you've given me a solution to one of my problems anyway! Thanks =]
glosrfc
May 1st, 2009, 08:19 PM
You need to reset the Boolean variable, "playing", depending on the conditional states of each of the buttons. For example, you need to consider whether playing is true or false when the Stop button is pressed (similar to the code I posted above). Obviously, if playing == true, then you need to stop the sound AND reset playing to false. But if playing != true, you don't have to do anything because it's already stopped.
The simplest comparison is that "playing" represents a lightswitch that can be either on or off. When you enter a room you check if the lights are on. If they're not, you turn them on with the switch (i.e. set "playing" = true). So think of the lights as your sound. When you leave the room (i.e. the sound has finished), you have to flick the switch to turn the lights off again or "playing" = false.
Do a search on this forum or elsewhere for boolean logic and you should find loads more examples.
Don't worry about adding normal (not key) frames to the end. They'll be blank anyway and won't add to the SWF size. But you should be able to visually see a representation of the entire waveform in the Flash editor timeline panel and this will help spot where the sound file finishes. That's where you place an empty keyframe and insert the stop() command.
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.