# Added sound to a program and now it crashes chrome

**URL:** <https://discourse.processing.org/t/added-sound-to-a-program-and-now-it-crashes-chrome/14946>\
**Category:** Libraries\
**Created:** [October 25, 2019, 4:35pm UTC](https://discourse.processing.org/t/added-sound-to-a-program-and-now-it-crashes-chrome/14946 "2019-10-25T16:35:43Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mike](https://avatars.discourse-cdn.com/v4/letter/m/58f4c7/32.png) [@Mike](https://discourse.processing.org/u/Mike)\
**Post date:** [October 25, 2019, 4:35pm UTC](https://discourse.processing.org/t/added-sound-to-a-program-and-now-it-crashes-chrome/14946/1 "2019-10-25T16:35:43Z")

</div>

Hey,  
I am making a project for my son who loves trains. It is all working decent at this time but as soon as i added the sound to the project and im running one of the mouse presses with the sound on it crashes chrome. Is there a way around this or am i asking too much of the browser???  
[https://editor.p5js.org/mcfiorino624/sketches/xVaLKQ\_NF](https://editor.p5js.org/mcfiorino624/sketches/xVaLKQ_NF)  
if you // the mySound.play();  
the program runs fine.

---

<div class="post-metadata">

**Author:** ![Mike](https://avatars.discourse-cdn.com/v4/letter/m/58f4c7/32.png) [@Mike](https://discourse.processing.org/u/Mike)\
**Post date:** [October 26, 2019, 2:12am UTC](https://discourse.processing.org/t/added-sound-to-a-program-and-now-it-crashes-chrome/14946/2 "2019-10-26T02:12:37Z")

</div>

Tried lowering the volume and trying a different file type instead of mp3 and still dont seem to have any luck. any ideas?

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [October 26, 2019, 2:40am UTC](https://discourse.processing.org/t/added-sound-to-a-program-and-now-it-crashes-chrome/14946/3 "2019-10-26T02:40:00Z")

</div>

i know you not want to hear that:

-a- please clean up your coding so we find

- where is the sound variable declared,
- where is the sound file loaded
- where is the sound started

your new multi-file.js structure is NOT HELPING  
( we talked about that in your last topic, where we disagree HOW to avoid 3000 line coding )

-b-  
sound you start like in setup

> **[reference | p5.js](https://p5js.org/reference/#/p5.SoundFile/loop)**
>
> p5.js a JS client-side library for creating graphic and interactive experiences, based on the core principles of Processing.

and directly pause it

> **[reference | p5.js](https://p5js.org/reference/#/p5.SoundFile/pause)**
>
> p5.js a JS client-side library for creating graphic and interactive experiences, based on the core principles of Processing.

* * *

later if you want to

- start you need to check if running first so something like  
`if ( ! mySound.isLooping() ) mySound.loop();`

- stop use pause again  
`if ( mySound.isLooping() ) mySound.pause();`

if you send play ( or loop ) on ( while ) one of your game conditions  
or stop() or pause() in a other condition  
you actually send it 50 times per sec to the computer OS

> [@Mike](#):
>
> am i asking too much of the browser

insofar YES

one of the things about modules is that you (can) make the sound code  
in one module ( function ) ( even one extra file if you want to use it in a other program again )  
but you need to call that from main ( and document that for other readers )

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [October 26, 2019, 2:40am UTC](https://discourse.processing.org/t/added-sound-to-a-program-and-now-it-crashes-chrome/14946/4 "2019-10-26T02:40:16Z")

</div>

> **[Reference](https://p5js.org/reference/#/p5.sound/getAudioContext)**
>
> Find easy explanations for every piece of p5.js code.

> [@Pls critique my code!](https://discourse.processing.org/t/pls-critique-my-code/12855/2):
>
> Not at all! Callback preload() is the single best place to load assets. star_struck BtW, you’ve got some missing indentation on your posted code. woozy_face We can use [Beautifier.io](http://Beautifier.io) in order to clean up our JS code. wink Here’s my own take on your sketch (Warning: Not fully tested!): sunglassesindex.html: \<!DOCTYPE html\> \<meta charset=utf-8\> \<meta name=viewport content=width=device-width,initial-scale=1\> \<script defer src=https://cdn.JsDelivr.net/npm/p5\>\</script\> \<script defer s…

---

<div class="post-metadata">

**Author:** ![Mike](https://avatars.discourse-cdn.com/v4/letter/m/58f4c7/32.png) [@Mike](https://discourse.processing.org/u/Mike)\
**Post date:** [October 26, 2019, 2:45am UTC](https://discourse.processing.org/t/added-sound-to-a-program-and-now-it-crashes-chrome/14946/5 "2019-10-26T02:45:29Z")

</div>

i thought you wanted me to separate everything into separate .js files so it would look cleaner.  
I think this looks way better than the first game haahha.  
so should i not call the audio in the draw function? how do i time it correctly then if it is meant to follow the rising of the barrior?

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [October 26, 2019, 2:48am UTC](https://discourse.processing.org/t/added-sound-to-a-program-and-now-it-crashes-chrome/14946/6 "2019-10-26T02:48:31Z")

</div>

> [@Mike](#):
>
> you wanted me to separate everything into separate .js files so it would look cleaner

please link to where i say that?

i talked about to make:

```auto
function myfunction(){
  doSomething();
}

```

and if you check your first program again, you might understand that a  
`mouseOver(x,y,w,h) { return true/false } `  
would have saved you lots of coding.

* * *

anyhow,  
from draw you want to start sound?  
yes, as i show the logic above,

- 
  - only start if not running

- 
  - only stop if running

---

<div class="post-metadata">

**Author:** ![Mike](https://avatars.discourse-cdn.com/v4/letter/m/58f4c7/32.png) [@Mike](https://discourse.processing.org/u/Mike)\
**Post date:** [October 26, 2019, 2:59am UTC](https://discourse.processing.org/t/added-sound-to-a-program-and-now-it-crashes-chrome/14946/7 "2019-10-26T02:59:14Z")

</div>

Sorry, sometimes it not the easiest to understand all of your english.  
I want to start the sound during a keypress when the angle variable is between a certain point.  
if its outside that variable i want it to shut off.  
i understand having it in draw has it starting and stopping 50 times a second like you said but the function for the angle is in draw so i tried putting it there so i wouldnt have to add more if statements.

---

<div class="post-metadata">

**Author:** ![Mike](https://avatars.discourse-cdn.com/v4/letter/m/58f4c7/32.png) [@Mike](https://discourse.processing.org/u/Mike)\
**Post date:** [October 26, 2019, 3:01am UTC](https://discourse.processing.org/t/added-sound-to-a-program-and-now-it-crashes-chrome/14946/8 "2019-10-26T03:01:28Z")

</div>

I am still very new to this, I used the .js files to build certain parts of the program. train.js has the image information and puts it in multiple functions to show and move the train. this is what i thought you meant by modular.

---

<div class="post-metadata">

**Author:** ![Mike](https://avatars.discourse-cdn.com/v4/letter/m/58f4c7/32.png) [@Mike](https://discourse.processing.org/u/Mike)\
**Post date:** [October 26, 2019, 3:27am UTC](https://discourse.processing.org/t/added-sound-to-a-program-and-now-it-crashes-chrome/14946/9 "2019-10-26T03:27:26Z")

</div>

Now I have the sound starting to loop in setup. I have an if statement in draw stating if the angle variable is between -65 degrees and -2 degrees i want it to play, else i want it to pause.  
now the audio is not crashing the program but it sounds all choppy and cracking.

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [October 26, 2019, 3:27am UTC](https://discourse.processing.org/t/added-sound-to-a-program-and-now-it-crashes-chrome/14946/10 "2019-10-26T03:27:48Z")

</div>

example from a modified version of code you studied already???  
[https://editor.p5js.org/kll/sketches/NIwFX54Gk](https://editor.p5js.org/kll/sketches/NIwFX54Gk)

and yes use extra myTools.js ( as long it is self contained and understandable )

---

<div class="post-metadata">

**Author:** ![Mike](https://avatars.discourse-cdn.com/v4/letter/m/58f4c7/32.png) [@Mike](https://discourse.processing.org/u/Mike)\
**Post date:** [October 26, 2019, 3:30am UTC](https://discourse.processing.org/t/added-sound-to-a-program-and-now-it-crashes-chrome/14946/11 "2019-10-26T03:30:51Z")

</div>

Im trying to comprehend all of this but im like 2 weeks in, your programs are far more advanced than anything my brain can understand haha.

---

<div class="post-metadata">

**Author:** ![Mike](https://avatars.discourse-cdn.com/v4/letter/m/58f4c7/32.png) [@Mike](https://discourse.processing.org/u/Mike)\
**Post date:** [October 26, 2019, 3:38am UTC](https://discourse.processing.org/t/added-sound-to-a-program-and-now-it-crashes-chrome/14946/12 "2019-10-26T03:38:16Z")

</div>

my other issue with this program is I have the railroad crossing barrier working with the up and down arrow presses and they are nice and fluid, but they have a set value they should not go past for the angle.  
This doesn’t work unless it detects the key is release. Is it possible to have the program stop the function without key release

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [October 26, 2019, 3:38am UTC](https://discourse.processing.org/t/added-sound-to-a-program-and-now-it-crashes-chrome/14946/13 "2019-10-26T03:38:16Z")

</div>

ok, just bookmark them for later

---

<div class="post-metadata">

**Author:** ![Mike](https://avatars.discourse-cdn.com/v4/letter/m/58f4c7/32.png) [@Mike](https://discourse.processing.org/u/Mike)\
**Post date:** [October 26, 2019, 3:38am UTC](https://discourse.processing.org/t/added-sound-to-a-program-and-now-it-crashes-chrome/14946/14 "2019-10-26T03:38:54Z")

</div>

i did i have saved all your helpful links you sent me 😀
