Sound in games distorts

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?

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.

6 Likes

Susan,

Great problem solving!

Brad

1 Like

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

@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

1 Like

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

L11 Conditional project

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.


Can you tell me what the ‘false’ is in the playSound?
Thank you!

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

2 Likes

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

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

Hi Michael – so sorry I forgot to attach the link

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

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.

Mike