@brandy
In p5.js, you can play sounds by using the p5.sound library.
Here is a simple example:
1 2 |
<script src="https://cdn.jsdelivr.net/npm/p5"></script> <script src="https://cdn.jsdelivr.net/npm/p5/lib/addons/p5.sound.js"></script> |
1 2 3 4 5 |
let soundFile; function preload() { soundFile = loadSound('path-to-your-sound-file.mp3'); } |
1 2 3 4 5 6 |
function setup() { createCanvas(400, 400); // Play the sound when the sketch starts soundFile.play(); } |
You can also stop the sound using the stop()
method, pause it using the pause()
method, or change its volume using the setVolume()
method.
Remember to check the console for any errors and make sure your sound file path is correct.