# Sound in games distorts

**URL:** https://forum.code.org/t/sound-in-games-distorts/23941
**Category:** CS Discoveries
**Created:** [January 10, 2019, 4:39pm UTC](https://forum.code.org/t/sound-in-games-distorts/23941 "2019-01-10T16:39:40Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![sdiaz](https://avatars.discourse-cdn.com/v4/letter/s/50afbb/32.png) [@sdiaz](https://forum.code.org/u/sdiaz)
#### Post date: [January 10, 2019, 4:39pm UTC](https://forum.code.org/t/sound-in-games-distorts/23941/1 "2019-01-10T16:39:40Z")

</div>

A team is adding music to their game. It sounds fine when they place the sound OUTSIDE the draw loop. It is super distorted to the point of static if they place it INSIDE the draw loop. They need it inside the loop to control when it plays. We can’t figure out why it’s distorted. Any ideas?

---

<div class="post-metadata">

### Author: ![sdiaz](https://avatars.discourse-cdn.com/v4/letter/s/50afbb/32.png) [@sdiaz](https://forum.code.org/u/sdiaz)
#### Post date: [January 10, 2019, 6:47pm UTC](https://forum.code.org/t/sound-in-games-distorts/23941/2 "2019-01-10T18:47:27Z")

</div>

Figured it out! Well, this works:

function draw() {  
playSound(“crab.mp3”, false);  
**stopSound(“crab.mp3”);**

They hadn’t stopped the sound. Adding that line of code makes it not distort inside the draw loop.

---

<div class="post-metadata">

### Author: ![bradleywellsashley](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/bradleywellsashley/32/522_2.png) [@bradleywellsashley](https://forum.code.org/u/bradleywellsashley)
#### Post date: [January 13, 2019, 3:32pm UTC](https://forum.code.org/t/sound-in-games-distorts/23941/3 "2019-01-13T15:32:26Z")

</div>

Susan,

Great problem solving!

Brad

---

<div class="post-metadata">

### Author: ![sdiaz](https://avatars.discourse-cdn.com/v4/letter/s/50afbb/32.png) [@sdiaz](https://forum.code.org/u/sdiaz)
#### Post date: [January 22, 2019, 2:34am UTC](https://forum.code.org/t/sound-in-games-distorts/23941/4 "2019-01-22T02:34:42Z")

</div>

I’m throwing this back out there for more feedback. This solution doesn’t work consistently? Any ideas what we are doing incorrectly?

---

<div class="post-metadata">

### Author: ![elizabeth\_admin](https://avatars.discourse-cdn.com/v4/letter/e/bb73d2/32.png) [@elizabeth\_admin](https://forum.code.org/u/elizabeth_admin)
#### Post date: [January 22, 2019, 7:55pm UTC](https://forum.code.org/t/sound-in-games-distorts/23941/5 "2019-01-22T19:55:52Z")

</div>

@sdiaz

Hi Susan,

I think the main problem is that the sound is restarting every time you run the draw loop, which is causing distortion. It’s really hard to know without a link to the actual project, though. Can you click the “Share” button and paste the link here so other people try to help?

Thanks,  
Elizabeth

---

<div class="post-metadata">

### Author: ![sdiaz](https://avatars.discourse-cdn.com/v4/letter/s/50afbb/32.png) [@sdiaz](https://forum.code.org/u/sdiaz)
#### Post date: [April 9, 2019, 9:11pm UTC](https://forum.code.org/t/sound-in-games-distorts/23941/6 "2019-04-09T21:11:49Z")

</div>

Different semester, same problem. playSound is in line 22 of the code.

[L11 Conditional project](https://studio.code.org/projects/gamelab/0bxFBHz5QUQvNZrzv1V863GgjVFg9wCLTplPSMVHuRo)

---

<div class="post-metadata">

### Author: ![edavis](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/edavis/32/4927_2.png) [@edavis](https://forum.code.org/u/edavis)
#### Post date: [April 10, 2019, 10:09pm UTC](https://forum.code.org/t/sound-in-games-distorts/23941/7 "2019-04-10T22:09:29Z")

</div>

So the problem is the same as stated above. The sound is restarting every cycle of the draw loop. This creates the distortion. The code would need to include a stop to the sound. Look at the posts above for some examples on how to do this.

---

<div class="post-metadata">

### Author: ![jillbrandeberry](https://avatars.discourse-cdn.com/v4/letter/j/8491ac/32.png) [@jillbrandeberry](https://forum.code.org/u/jillbrandeberry)
#### Post date: [May 8, 2019, 7:30pm UTC](https://forum.code.org/t/sound-in-games-distorts/23941/8 "2019-05-08T19:30:11Z")

</div>

![image](https://us1.discourse-cdn.com/flex016/uploads/codeorgforum/original/2X/7/7520ad34090ef2338bb2360a477599e914490e44.png)  
Can you tell me what the ‘false’ is in the playSound?  
Thank you!

---

<div class="post-metadata">

### Author: ![sdiaz](https://avatars.discourse-cdn.com/v4/letter/s/50afbb/32.png) [@sdiaz](https://forum.code.org/u/sdiaz)
#### Post date: [May 8, 2019, 7:45pm UTC](https://forum.code.org/t/sound-in-games-distorts/23941/9 "2019-05-08T19:45:31Z")

</div>

Setting that parameter to “false” means “no loop”

---

<div class="post-metadata">

### Author: ![andrea.burgess](https://avatars.discourse-cdn.com/v4/letter/a/fbc32d/32.png) [@andrea.burgess](https://forum.code.org/u/andrea.burgess)
#### Post date: [April 28, 2020, 10:09pm UTC](https://forum.code.org/t/sound-in-games-distorts/23941/10 "2020-04-28T22:09:03Z")

</div>

When we do this, it works on the first run of the game, but on subsequent runs, the sound does not play at all.  
I was thinking it might be the speed at which the 2 functions were running, so played around with changing the framerate inside that If loop - but we are getting the same behavior.  
☹

---

<div class="post-metadata">

### Author: ![mkmietowicz](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/mkmietowicz/32/6322_2.png) [@mkmietowicz](https://forum.code.org/u/mkmietowicz)
#### Post date: [April 29, 2020, 3:45am UTC](https://forum.code.org/t/sound-in-games-distorts/23941/11 "2020-04-29T03:45:32Z")

</div>

@andrea.burgess have you stopped the program and then restarted it, or is this all in one “run” of the program? If you share a link one of us can take a look at the code.

Thanks,  
–Michael K.

---

<div class="post-metadata">

### Author: ![andrea.burgess](https://avatars.discourse-cdn.com/v4/letter/a/fbc32d/32.png) [@andrea.burgess](https://forum.code.org/u/andrea.burgess)
#### Post date: [April 30, 2020, 6:34pm UTC](https://forum.code.org/t/sound-in-games-distorts/23941/12 "2020-04-30T18:34:31Z")

</div>

Hi Michael – so sorry I forgot to attach the link

[https://studio.code.org/projects/gamelab/SDmGrdKz-ai0D6aLNL6vVXz1ZBmUJnCM\_92a7ajM50I](https://studio.code.org/projects/gamelab/SDmGrdKz-ai0D6aLNL6vVXz1ZBmUJnCM_92a7ajM50I)

It works as expected the first time the page is loaded.

If we Reset and Run again – the you\_win sound does not play when the score reaches 3.

If we reload the web page, it will again work – but only the first time we run.

Hope that makes sense?

Thank you! J

---

<div class="post-metadata">

### Author: ![mwood](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/mwood/32/4941_2.png) [@mwood](https://forum.code.org/u/mwood)
#### Post date: [May 1, 2020, 8:38pm UTC](https://forum.code.org/t/sound-in-games-distorts/23941/13 "2020-05-01T20:38:54Z")

</div>

That is a little strange since everything should reset when you press the reset button and start the game over … It may have something to do with the stopSound() block. Of course simply removing it causes “you win” to infinitely repeat.

I am blatantly stealing a solution from a colleague (@melynn) who just posted this for a similar problem, but check out this version of your program which seems to work …

I added a sound variable (at the top of the program) and then once the winning sequence is triggered, it augments the count of the sound variable and only triggers the sound one time as part of the loop.

See if this will work for you.

> **[Check out what I made](https://studio.code.org/projects/gamelab/G0xzzz3GiSODeuQJKirfSvG_wRSGIiBVJrnRxAfU6l0)**
>
> I wrote the code myself with Code.org

Mike
