# Flash MX soundobject help

**URL:** https://forum.kirupa.com/t/flash-mx-soundobject-help/13657
**Category:** flash
**Created:** [February 16, 2003, 12:21pm UTC](https://forum.kirupa.com/t/flash-mx-soundobject-help/13657 "2003-02-16T12:21:51Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Danboy](https://avatars.discourse-cdn.com/v4/letter/d/f9ae1b/32.png) [@Danboy](https://forum.kirupa.com/u/Danboy)
#### Post date: [February 16, 2003, 12:21pm UTC](https://forum.kirupa.com/t/flash-mx-soundobject-help/13657/1 "2003-02-16T12:21:51Z")

</div>

Hi folks,

I need a bit of help here - I’ve followed Kirupa’s tutorial at [http://www.kirupa.com/developer/actionscript/sound.htm](http://www.kirupa.com/developer/actionscript/sound.htm), and have attached this piece of code to my start button as it says:

on (release) {  
kirupaSound = new Sound(this);  
kirupaSound.attachSound(“coolsound”);  
kirupaSound.start(0, 99);  
}

Everything seems fine and dandy. and my sound loop starts playing. Just one question though - what actionscript do you use to turn the sound off?!

Any help would be greatly appreciated, and would perhaps delay the onset of a nervous breakdown! :crazy:

Cheers,

Dan

---

<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: [February 16, 2003, 2:32pm UTC](https://forum.kirupa.com/t/flash-mx-soundobject-help/13657/2 "2003-02-16T14:32:58Z")

</div>

did you try:

kirupaSound.stop();

---

<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: [February 16, 2003, 2:49pm UTC](https://forum.kirupa.com/t/flash-mx-soundobject-help/13657/3 "2003-02-16T14:49:52Z")

</div>

I did, but it generated this error message:

Scene=Scene 1, Layer=stop, Frame=1: Line 1: Statement must appear within on handler  
kirupaSound.stop();

As you’ve probably guessed, I’m extremely new to Actionscripting within Flash. All I want to do is start and stop a piece of music that I’ve written within my webpage - it starts okay, but it’s stopping it that’s been the problem! ☹

Have a look at the page here - [www.fletchermunsoneffect.co.uk/indexone.htm](http://www.fletchermunsoneffect.co.uk/indexone.htm) and you’ll see what I’m trying to do. The page isn’t completely finished - the rollovers and links don’t work yet, but you’ll get the basic idea.

Also, if you press play again, the music starts looping again over the original loop. How can I prevent this?

Thanks for your help,

Dan

---

<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: [February 16, 2003, 2:52pm UTC](https://forum.kirupa.com/t/flash-mx-soundobject-help/13657/4 "2003-02-16T14:52:18Z")

</div>

> Scene=Scene 1, Layer=stop, Frame=1: Line 1: Statement must appear within on handler  
> kirupaSound.stop();

Well your adding the code to your button without an ‘on’ handler:

Play Button Actions:

on (release) {  
kirupaSound = new Sound(this);  
kirupaSound.attachSound(“coolsound”);  
kirupaSound.start(0, 99);  
}

Stop Button Actions:k

on (release) {  
kirupaSound.stop();  
}

---

<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: [February 16, 2003, 3:10pm UTC](https://forum.kirupa.com/t/flash-mx-soundobject-help/13657/5 "2003-02-16T15:10:21Z")

</div>

Here is a solution that would use ONE Button to play, and stop the sound…

kirupaSound = new Sound(this);  
kirupaSound.attachSound(“coolsound”);  
myButton.onPress = function() {//Change myButton to your button’s instance name  
x++;  
kirupaSound.start(0, 99);  
if (x\>1) {  
kirupaSound.stop();  
delete x;  
}  
};

Untested, because I have no flash on this pc, tell me how it goes. Please attach this code in your Frame Actions…

---

<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: [February 16, 2003, 4:04pm UTC](https://forum.kirupa.com/t/flash-mx-soundobject-help/13657/6 "2003-02-16T16:04:11Z")

</div>

h88, you’re a star.

I attached the code to the stop button and everything works fine. Thanks a million.

One more question and I promise I’ll leave you alone! 😏

How would I prevent the sound starting again if the play button is pressed a second time before the stop button? Any ideas?

Thanks again,

Dan

---

<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: [February 16, 2003, 4:15pm UTC](https://forum.kirupa.com/t/flash-mx-soundobject-help/13657/7 "2003-02-16T16:15:25Z")

</div>

Hey, didn’t I answer your question in the above post…?

Anyways, you might want to use two buttons instead tho, if so, try:

kirupaSound = new Sound(this);  
kirupaSound.attachSound(“coolsound”);  
PlayButton.onPress = function() {  
\_global.x++;  
kirupaSound.start(0, 99);  
if (x\>1) {  
this.enabled = false  
\_root.StopButton.enabled = true;  
}  
};  
StopButton.onPress = function() {  
if (x\>1) {  
kirupaSound.stop();  
this.enabled = false  
\_root.PlayButton.enabled = 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: [February 16, 2003, 4:47pm UTC](https://forum.kirupa.com/t/flash-mx-soundobject-help/13657/8 "2003-02-16T16:47:05Z")

</div>

Oops! Sorry about that - it’s been a long day!

I’ve sorted it now thanks to you and also the Flash 5 tutorials on the ultrashock site ([www.ultrashock.com](http://www.ultrashock.com)).

Cheers again,

Dan
