Can anyone help me out with a problem I have regarding
actionscript. What I am trying to do is create a button that
switches a streaming mp3 file on/off (Stop/Play).
I want a single button that if pressed when the music is
playing, switches off the music. If pressed when the music
has been stopped, plays the music again.
I am stuck because I don’t know how to keep track of if the
music is on or off. At the beginning of the movie the music
will play automatically, I have placed the code on a seperate layer in the main timeline. I was thinking of setting a
variable called ‘play’ to true at the beginning then I
realised that because the way flash works the variable will
be reset each time the play head gets to the beginning of
the movie.
I am new to flash and especially new to actionscript, can
anyone help me out with this?
Code :
mySound = new Sound();
mySound.loadSound(“loop.mp3”, isStreaming);
mySound.start();
All this does is play/stream the mp3 file on startup.
Thanks for helping me out but that still doesn’t really solve my problem.
All I am after is one button that is going to act as a on/off switch. I only want one button so I need to have some kind of way to keep track of if the music is currently playing. Otherwise I will not know if I should be playing the music or stopping it.
You can do this by changing the button itself, such as hiding one button and showing another, or putting the button in a movie clip and jumping to another frame where it has another script. But a simpler way is to use a variable flag in the script to tell which state the button is currently in. Here’s an example:
on (release) {
// Toggle a flag between TRUE and FALSE:
IamActive = not IamActive;
// Do different things depending on flag's current state:
if (IamActive) {
// various actions to be performed for "on" state of button
} else {
// various actions to be performed for "off" state of button
}
}
*Originally posted by indojepang *
**(taken from macromedia.com)
on (release) {
// Toggle a flag between TRUE and FALSE:
IamActive = not IamActive;
// Do different things depending on flag’s current state:
if (IamActive) {
// various actions to be performed for “on” state of button
} else {
// various actions to be performed for “off” state of button
}
}
**
Thanks for the reply.
I'm still a bit confused what the following line does 'IamActive = not IamActive;'. Is 'IamActive' a property of a button?
Thanks for the tip about swapping the buttons around. I think I have figured out a way to do it.
(1). Movie starts with music playing
(2). Playing Button is shown.
Playing Button :-
'On Release'
Stop Music
Hide Playing Button
Show Off Button'
Off Button :-
'On Release'
Play Music
Hide Off Button
Show Playing Button
This will work won't it?
Thanks.
[AS]
on(release){
if(playing!=true){ // if playing is not true… meaning if our mp3 is not loaded
x.loadSound(“na.mp3”,true);// load our mp3
x.start(0,999);
playing = true;// declaring that our mp3 is in fact playing
}
}
[/AS]
I want a single button that if pressed when the music is
playing, switches off the music. If pressed when the music
has been stopped, plays the music again.
Thanks to everyone who has taken the time to help me out. I have now got it working exactly how I wanted it
As indojepang suggested I used the swap buttons technique. Basically it works as follows:
Movie loads with a streaming mp3 file (400k) playing in the background.
A animated button is shown to represent the music play, in my case it’s a pair of earphones with a few wavy lines animating in the middle.
The user can then click on the button to turn of the music. When the button is clicked , the music is turned off and the animated buttons visibility is set to false (to hide it). Another button underneath is then made visible in exactly the same space as the previous button.
When the user clicks this new button, the music starts playing again. The current button is hidden and the animated button is shown again.
Simple =)
It looks like one button is being used to turn the sound on and off but it’s actually two.
Digitalosophy :
but recheck your isStreaming code. that is wrong, replace with true;
I think you can either specify true or just enter ‘isStreaming’. I got this from the FlashMX help guide. I think they both do the same.
indojepang:
Love the speaker animation, do you mind if I use it or copy it and make a few little changes. At the moment I have a animated headphone but i’m not sure if I like the look of it yet.
Anyway thanks yet again to everyone who helped me out. I am working on my first ever flash site and am slowly getting there. I’m sure i’ll be back soon asking how to create a preloader =)