# Flash MX Virgin - On/Off switch for streaming mp3 file

**URL:** <https://forum.kirupa.com/t/flash-mx-virgin-on-off-switch-for-streaming-mp3-file/24958>\
**Category:** flash\
**Created:** [July 7, 2003, 5:52pm UTC](https://forum.kirupa.com/t/flash-mx-virgin-on-off-switch-for-streaming-mp3-file/24958 "2003-07-07T17:52:46Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![cheddar](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/cheddar/32/267_2.png) [@cheddar](https://forum.kirupa.com/u/cheddar)\
**Post date:** [July 7, 2003, 5:52pm UTC](https://forum.kirupa.com/t/flash-mx-virgin-on-off-switch-for-streaming-mp3-file/24958/1 "2003-07-07T17:52:46Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 7, 2003, 5:57pm UTC](https://forum.kirupa.com/t/flash-mx-virgin-on-off-switch-for-streaming-mp3-file/24958/2 "2003-07-07T17:57:57Z")

</div>

Take a look at this…

[http://www.kirupaforum.com/forums/showthread.php?s=&threadid=24137&highlight=mp3+on+off](http://www.kirupaforum.com/forums/showthread.php?s=&threadid=24137&highlight=mp3+on+off)

TD

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 7, 2003, 6:13pm UTC](https://forum.kirupa.com/t/flash-mx-virgin-on-off-switch-for-streaming-mp3-file/24958/3 "2003-07-07T18:13:01Z")

</div>

> \*Originally posted by tobydog \*  
> \*\*Take a look at this…

[http://www.kirupaforum.com/forums/showthread.php?s=&threadid=24137&highlight=mp3+on+off](http://www.kirupaforum.com/forums/showthread.php?s=&threadid=24137&highlight=mp3+on+off)

\*\*

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.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 7, 2003, 8:17pm UTC](https://forum.kirupa.com/t/flash-mx-virgin-on-off-switch-for-streaming-mp3-file/24958/4 "2003-07-07T20:17:40Z")

</div>

(taken from [macromedia.com](http://macromedia.com))

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:

```auto

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
   }

}

```

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 7, 2003, 8:21pm UTC](https://forum.kirupa.com/t/flash-mx-virgin-on-off-switch-for-streaming-mp3-file/24958/5 "2003-07-07T20:21:10Z")

</div>

btw… when will you’re not a virgin anymore?? 😛

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 7, 2003, 8:39pm UTC](https://forum.kirupa.com/t/flash-mx-virgin-on-off-switch-for-streaming-mp3-file/24958/6 "2003-07-07T20:39:06Z")

</div>

> \*Originally posted by indojepang \*  
> \*\*(taken from [macromedia.com](http://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  
}

}

```auto
 **
 

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.
```

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 7, 2003, 8:57pm UTC](https://forum.kirupa.com/t/flash-mx-virgin-on-off-switch-for-streaming-mp3-file/24958/7 "2003-07-07T20:57:44Z")

</div>

[http://www.delusionalfx.com/tutorials/xternalsound.htm](http://www.delusionalfx.com/tutorials/xternalsound.htm)

cant really explain much better then there. follow my article (which it looks like you did), and post your fla, i will help you

but recheck your isStreaming code. that is wrong, replace with true;

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 7, 2003, 8:58pm UTC](https://forum.kirupa.com/t/flash-mx-virgin-on-off-switch-for-streaming-mp3-file/24958/8 "2003-07-07T20:58:23Z")

</div>

in case ur lazy 🙂

[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]

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 7, 2003, 9:02pm UTC](https://forum.kirupa.com/t/flash-mx-virgin-on-off-switch-for-streaming-mp3-file/24958/9 "2003-07-07T21:02:51Z")

</div>

> 

or putting the button in a movie clip and jumping to another frame where it has another script

take a look at the fla…

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 7, 2003, 9:06pm UTC](https://forum.kirupa.com/t/flash-mx-virgin-on-off-switch-for-streaming-mp3-file/24958/10 "2003-07-07T21:06:06Z")

</div>

> \*Originally posted by indojepang \*  
> \*\*take a look at the fla… \*\*

i believe he is trying to load full mp3’s

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 7, 2003, 9:28pm UTC](https://forum.kirupa.com/t/flash-mx-virgin-on-off-switch-for-streaming-mp3-file/24958/11 "2003-07-07T21:28:03Z")

</div>

> 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.

don’t care about loading thing…😛

hey Digital!!! are u a singer??

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 7, 2003, 9:31pm UTC](https://forum.kirupa.com/t/flash-mx-virgin-on-off-switch-for-streaming-mp3-file/24958/12 "2003-07-07T21:31:18Z")

</div>

well either way will work:) i try not to load and unload sound by going to different frames, but you can. I do it via AS as it is much neater

Oh and no I am not a singer, I just love music

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 7, 2003, 9:38pm UTC](https://forum.kirupa.com/t/flash-mx-virgin-on-off-switch-for-streaming-mp3-file/24958/13 "2003-07-07T21:38:15Z")

</div>

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:

1. Movie loads with a streaming mp3 file (400k) playing in the background.

2. 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.

3. 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.

4. 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 =)

Cheers.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 7, 2003, 9:40pm UTC](https://forum.kirupa.com/t/flash-mx-virgin-on-off-switch-for-streaming-mp3-file/24958/14 "2003-07-07T21:40:02Z")

</div>

glad to see it’s working

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 7, 2003, 9:53pm UTC](https://forum.kirupa.com/t/flash-mx-virgin-on-off-switch-for-streaming-mp3-file/24958/15 "2003-07-07T21:53:07Z")

</div>

the speaker animation? be my guest…

i bet Digitalosophy love hip & hardcore…🙂

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 7, 2003, 9:58pm UTC](https://forum.kirupa.com/t/flash-mx-virgin-on-off-switch-for-streaming-mp3-file/24958/16 "2003-07-07T21:58:18Z")

</div>

hip hop was good, not sure what happened lately 🙂 rok is where it’s at
