# I can't seem to play sounds in javascript using p5.js

**URL:** <https://discourse.processing.org/t/i-cant-seem-to-play-sounds-in-javascript-using-p5-js/29043>\
**Category:** Libraries\
**Created:** [April 1, 2021, 6:11am UTC](https://discourse.processing.org/t/i-cant-seem-to-play-sounds-in-javascript-using-p5-js/29043 "2021-04-01T06:11:11Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![LaserDenis](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/laserdenis/32/12685_2.png) [@LaserDenis](https://discourse.processing.org/u/LaserDenis)\
**Post date:** [April 1, 2021, 6:11am UTC](https://discourse.processing.org/t/i-cant-seem-to-play-sounds-in-javascript-using-p5-js/29043/1 "2021-04-01T06:11:11Z")

</div>

```
I am new to Js, and p5 and I was trying to play a mp3 from my computer, but I continually get this errors:
Uncaught (in promise) ReferenceError: loadSound is not defined
Uncaught TypeError: Cannot read property 'play' of undefined
[enter image description here][1]

```

I’m using Visual Studio Code. I was trying to follow an coding train tutorial but it doesn’t seems to work. Sorry if my English is poor.

```
  [1]: https://i.stack.imgur.com/NOoQn.jpg
The only thing I'm doing with the sound file is loadSound and .play(). I think maybe there are problems with my libraries and with index.html file. I'm new to p5 and js so if you need additional files I can post them afterwards.

```

> 

```
<html lang="">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>p5.js example</title>
  <style>
    body {
      padding: 0;
      margin: 0;
    }
  </style>
  <script src="../p5.js"></script>
  <!-- <script src="../addons/p5.sound.js"></script> -->
  <script src="https://cdn.jsdelivr.net/gh/bmoren/p5.collide2D/p5.collide2d.min.js"></script>
  <script src="sketch.js"></script>
  <script src="dio.js"></script> 
</head>
<body>
  <main>
  </main>
</body>
</html>
</>
    and this is part of the code in sketch.js
    let sound;
    function preload() {
      sound=loadSound('song.mp3');
    }
function setup() {![fku|690x76](upload://P2n8HRc7sGmvC5KBTNetHclIcT.jpeg) 
  createCanvas(1920, 500);
  sound.play();
}

```

this is were the error comes from, i don’t get errors if I comment loadSound and sound.play. Thins mp3 is just a local file from my computer. And do you know a site from where I cant copy the url so I can play sounds(like how you can post an image on imgur an then link it into the code.

---

<div class="post-metadata">

**Author:** ![two.ticks](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/two.ticks/32/12676_2.png) [@two.ticks](https://discourse.processing.org/u/two.ticks)\
**Post date:** [April 1, 2021, 6:35am UTC](https://discourse.processing.org/t/i-cant-seem-to-play-sounds-in-javascript-using-p5-js/29043/2 "2021-04-01T06:35:55Z")

</div>

> [@LaserDenis](#):
>
> `<!-- <script src="../addons/p5.sound.js"></script> -->`

Did you try uncommenting this? Maybe p5.sound.js is not included in your sketch.

---

<div class="post-metadata">

**Author:** ![LaserDenis](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/laserdenis/32/12685_2.png) [@LaserDenis](https://discourse.processing.org/u/LaserDenis)\
**Post date:** [April 1, 2021, 6:37am UTC](https://discourse.processing.org/t/i-cant-seem-to-play-sounds-in-javascript-using-p5-js/29043/3 "2021-04-01T06:37:49Z")

</div>

![eror](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/f/f07d15fa1f83dc7ae5be3740f39b242771c83d4d.jpeg) I tried but I get this errors

---

<div class="post-metadata">

**Author:** ![two.ticks](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/two.ticks/32/12676_2.png) [@two.ticks](https://discourse.processing.org/u/two.ticks)\
**Post date:** [April 1, 2021, 6:48am UTC](https://discourse.processing.org/t/i-cant-seem-to-play-sounds-in-javascript-using-p5-js/29043/4 "2021-04-01T06:48:56Z")

</div>

I think that is due to changed google auto-play policies

---

<div class="post-metadata">

**Author:** ![LaserDenis](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/laserdenis/32/12685_2.png) [@LaserDenis](https://discourse.processing.org/u/LaserDenis)\
**Post date:** [April 1, 2021, 6:54am UTC](https://discourse.processing.org/t/i-cant-seem-to-play-sounds-in-javascript-using-p5-js/29043/5 "2021-04-01T06:54:33Z")

</div>

How would I change it then?

---

<div class="post-metadata">

**Author:** ![LaserDenis](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/laserdenis/32/12685_2.png) [@LaserDenis](https://discourse.processing.org/u/LaserDenis)\
**Post date:** [April 1, 2021, 6:59am UTC](https://discourse.processing.org/t/i-cant-seem-to-play-sounds-in-javascript-using-p5-js/29043/6 "2021-04-01T06:59:26Z")

</div>

Or do you have the basic libraries for p5

---

<div class="post-metadata">

**Author:** ![two.ticks](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/two.ticks/32/12676_2.png) [@two.ticks](https://discourse.processing.org/u/two.ticks)\
**Post date:** [April 1, 2021, 7:59am UTC](https://discourse.processing.org/t/i-cant-seem-to-play-sounds-in-javascript-using-p5-js/29043/7 "2021-04-01T07:59:39Z")

</div>

If possible try it in web-editor. For me it is currently working fine in web-editor. I am trying to find a way to run it locally.

---

<div class="post-metadata">

**Author:** ![LaserDenis](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/laserdenis/32/12685_2.png) [@LaserDenis](https://discourse.processing.org/u/LaserDenis)\
**Post date:** [April 1, 2021, 8:14am UTC](https://discourse.processing.org/t/i-cant-seem-to-play-sounds-in-javascript-using-p5-js/29043/8 "2021-04-01T08:14:28Z")

</div>

> 

 I copiel the libraries from p5.js wediotor but i get even more errors![ds|690x82](upload://aLrxnKahdEUCdOcMzLCjIj5jJjN.jpeg)![ds|690x82](upload://aLrxnKahdEUCdOcMzLCjIj5jJjN.jpeg)

---

<div class="post-metadata">

**Author:** ![two.ticks](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/two.ticks/32/12676_2.png) [@two.ticks](https://discourse.processing.org/u/two.ticks)\
**Post date:** [April 1, 2021, 9:08am UTC](https://discourse.processing.org/t/i-cant-seem-to-play-sounds-in-javascript-using-p5-js/29043/9 "2021-04-01T09:08:32Z")

</div>

![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/1/138c5493999dec2be6cd1fcf8cd3877f823bf7fe.png)

I tried this.

---

<div class="post-metadata">

**Author:** ![LaserDenis](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/laserdenis/32/12685_2.png) [@LaserDenis](https://discourse.processing.org/u/LaserDenis)\
**Post date:** [April 1, 2021, 9:09am UTC](https://discourse.processing.org/t/i-cant-seem-to-play-sounds-in-javascript-using-p5-js/29043/10 "2021-04-01T09:09:57Z")

</div>

yea it work for me to on p5.js web editor

---

<div class="post-metadata">

**Author:** ![micuat](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/micuat/32/19407_2.png) [@micuat](https://discourse.processing.org/u/micuat)\
**Post date:** [April 1, 2021, 9:15am UTC](https://discourse.processing.org/t/i-cant-seem-to-play-sounds-in-javascript-using-p5-js/29043/11 "2021-04-01T09:15:35Z")

</div>

how do you host the sketch locally? If you are simply opening the file on the browser, it may not possible to load the sound file from the local disk. I usually use `live-server` from `npm` but you can use python or other tools to serve the page, for example:

[![](https://img.youtube.com/vi/UCHzlUiDD10/hqdefault.jpg "9.12: Local Server, Text Editor, JavaScript Console - p5.js Tutorial") ](https://www.youtube.com/watch?v=UCHzlUiDD10)

it seems there’s an extension for VSCode too

> **[Local server](https://github.com/processing/p5.js/wiki/Local-server)**
>
> p5.js is a client-side JS platform that empowers artists, designers, students, and anyone to learn to code and express themselves creatively on the web. It is based on the core principles of Proces...

---

<div class="post-metadata">

**Author:** ![two.ticks](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/two.ticks/32/12676_2.png) [@two.ticks](https://discourse.processing.org/u/two.ticks)\
**Post date:** [April 1, 2021, 9:33am UTC](https://discourse.processing.org/t/i-cant-seem-to-play-sounds-in-javascript-using-p5-js/29043/12 "2021-04-01T09:33:53Z")

</div>

@LaserDenis Try in live-server, it is working for me. Thanks @micuat

---

<div class="post-metadata">

**Author:** ![LaserDenis](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/laserdenis/32/12685_2.png) [@LaserDenis](https://discourse.processing.org/u/LaserDenis)\
**Post date:** [April 1, 2021, 9:34am UTC](https://discourse.processing.org/t/i-cant-seem-to-play-sounds-in-javascript-using-p5-js/29043/13 "2021-04-01T09:34:50Z")

</div>

i’m gonna try it in 2 hours right know I can’t do it, but it sound promising

---

<div class="post-metadata">

**Author:** ![LaserDenis](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/laserdenis/32/12685_2.png) [@LaserDenis](https://discourse.processing.org/u/LaserDenis)\
**Post date:** [April 1, 2021, 12:46pm UTC](https://discourse.processing.org/t/i-cant-seem-to-play-sounds-in-javascript-using-p5-js/29043/14 "2021-04-01T12:46:56Z")

</div>

My friend God bless your.
