Play sound file

Hello
I am newb in programming..., Processing, p5.js....

I am trying to do a ssuper simple thing: listen a sound file just once. I would like to play the file just one time when I move the mouse in one direction, for example on the right of the window.

if I try:

if(mouseX > width/2){ sound.play(); }

the file play for every pixel in horizontal location. How can I say "beyond the width/2 position, play the file one time and loop when it finished?

Thank you

Answers

  • You need a condition around sound.play() to check if it's already playing. The reference is a little vague but I would expect you could do this by checking against isPlaying(). Alternatively if once triggered you're just going to loop continuously use your own variable to 'debounce':

    var soundIsPlaying = false;
    
    setup() { 
      //...
    }
    
    draw() {
      if(mouseX > width/2 && !soundIsPlaying){
        sound.play();
        soundIsPlaying = true;
      }
    }
    
  • Super it works! I tried isPlaying() without good results.
    With sound.loop(); it repeats properly without overlap (but it does not work in Pocessing, it plays the sound once (??))

    thank you

Sign In or Register to comment.