P5.js sound and keyTyped() or keyPressed()

I tried the last demo that I posted with one of the files on the website that you referred to; I changed the name of the Vinyl file to ‘keyA.mp3’, uploaded it to the ‘assets’ folder, preloaded it, and it plays when I press ‘a’ on my keyboard.

yes! I tried this code and it finally works! thank you! I still need to figure out how to make it stop playing though, because when I click on A again it just play the same song over the one still playing. And I need to click on the rectangle before clicking A for it to work. Do you think it’s possible for me to change the rectangle to a better animation?? My original thought was to click on a key then a basic animation would play and the sound will play as well.

https://p5js.org/reference/#/libraries/p5.sound

Look at this reference and find a stop(). Put two buttons on your interface: 1) start 2) stop and hook them up accordingly. Instead of typing ‘a’ you’ll be pushing buttons for start/stop.

Just curious why it all of a sudden started working; what did you leave out?

oh but my project idea is to make keyboard keys as DPM (Drum Pad Machine), so when a key is pressed a sound will play, I need to find a way to make it play and stop. There are also so many start functions in the reference but okay I’ll find the suitable ones.
And it suddenly works because I didn’t use let value = 0 in the beginning I thought it wouldn’t be necessary. I didn’t add this part.
let value = 0;
function draw() {
fill(value);
rect(25, 25, 50, 50);

I can’t add more replies so I’ll just edit my response here, yes I’ll keep that in mind and thank you for your help!

I didn’t add this part

That’s why it’s important to show all of your code; it would have saved us both some time. Something to remember for future questions.

1 Like