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

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.

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

I tried but I get this errors

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

How would I change it then?

Or do you have the basic libraries for p5

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.

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

I tried this.

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

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:

it seems there’s an extension for VSCode too

2 Likes

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

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

1 Like

My friend God bless your.

1 Like