# Linking sound to button

**URL:** <https://forum.kirupa.com/t/linking-sound-to-button/301>\
**Category:** Uncategorized\
**Created:** [July 12, 2002, 7:54pm UTC](https://forum.kirupa.com/t/linking-sound-to-button/301 "2002-07-12T19:54:06Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![FnyBoy4021](https://avatars.discourse-cdn.com/v4/letter/f/d2c977/32.png) [@FnyBoy4021](https://forum.kirupa.com/u/FnyBoy4021)\
**Post date:** [July 12, 2002, 7:54pm UTC](https://forum.kirupa.com/t/linking-sound-to-button/301/1 "2002-07-12T19:54:06Z")

</div>

hi… i read the turorial on how to link the music usin layers and all…but how do i make it so when u click it plays my song… like a music on button…and when u click it music starts playing… thx for thehelp

---

<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 13, 2002, 2:18pm UTC](https://forum.kirupa.com/t/linking-sound-to-button/301/2 "2002-07-13T14:18:47Z")

</div>

Quite simple actually 🙂

First thing to do is export your sound to ActionScript from the library. Name it something relevant like “track\_1” or “intro\_song”. Then, attach the following code to your button:

```auto

on(release) {
  if(!mySound) {
    mySound = new Sound();
    mySound.attachSound("*exported_name*");
    _root.soundInit == false
  }

  if(_root.soundInit == false) {
    mySound.start(0, *numberOfLoops*);
    _root.soundInit = true;
  } else {
    mySound.stop(("*exported_name*");
    _root.soundInit = false;
  }
}

```

---

<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 15, 2002, 4:03am UTC](https://forum.kirupa.com/t/linking-sound-to-button/301/3 "2002-07-15T04:03:08Z")

</div>

okay. I do what you said but it won’t work unless I hit **Export in First Frame**. But, when I hit that my preloader doesn’t even show up.  
the sound button is in the second frame and the preloader is supposed to load it.  
also, if you figure this out will you explain to me how to make the sound stop too.

\_root.brian.thank\_you

---

<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 15, 2002, 4:39am UTC](https://forum.kirupa.com/t/linking-sound-to-button/301/4 "2002-07-15T04:39:04Z")

</div>

This is prolly creating the error sorry…:

> 

mySound.stop((“exported\_name”);

Should be

```auto

mySound.stop("exported_name");

```

There’s no need to export in first frame as far as I know. I’ve done this many times beofre without exporting in first frame and it works fine.

> 

} else {  
mySound.stop((“exported\_name”);  
\_root.soundInit = false;  
}

This will stop the sound if the sound is already playing.

-ptolemy

---

<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:** [October 27, 2002, 8:25pm UTC](https://forum.kirupa.com/t/linking-sound-to-button/301/5 "2002-10-27T20:25:02Z")

</div>

I just learned how to attach sound! 😛

Never tried that before 🙂

thx!!

---

<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:** [October 27, 2002, 8:46pm UTC](https://forum.kirupa.com/t/linking-sound-to-button/301/6 "2002-10-27T20:46:59Z")

</div>

I was wondering why you were posting in the F5 forum…

---

<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:** [October 27, 2002, 8:51pm UTC](https://forum.kirupa.com/t/linking-sound-to-button/301/7 "2002-10-27T20:51:12Z")

</div>

hehe 🙂

Stumbled in here via the search…

tried to find the attachsound thingy… the flashMX help didn’t do me much good…
