sound on a key press

edited January 2016 in Library Questions

hi guys, i'm working on my first interactive piece using makeymakey, i want to play a sound on a click / keyPressed. So far I've managed to get a sound played on a click, however it works only once then it stops, so if i try clicking again it doesn't play the sound again. Here's a bit of code i've written.

import ddf.minim.*;
import ddf.minim.analysis.*;
import ddf.minim.effects.*;
import ddf.minim.signals.*;
import ddf.minim.spi.*;
import ddf.minim.ugens.*;

import processing.sound.*;

Minim minim;
AudioPlayer one; //variable name

void setup(){
 // germanloop();
  size(500,500);
  minim = new Minim(this);
  one =minim.loadFile("german.wav");
  //germanloop();




}

void draw(){

  background(0);
  //sou =minim.loadFile("german.wav");

}

void mousePressed(){
 if (mousePressed == true);
  one.play();
}

void mouseReleased()
{
  if (mousePressed || true);
  one.close();

}

Answers

  • Maybe use pause() instead of close()? :-/

  • @GoToLoop i'd ideally like he sound to play again from the beginning on the next click, would pause() not just pause it?

  • There's also rewind(). Complete API here: http://code.Compartmental.net/minim/

  • edited January 2016

    i managed to get it running as i wanted: @GoToLoop thanks for tips

    void mousePressed(){
     if (mousePressed == true);
      one.play();
    }
    
    void mouseReleased()
    {
      if (mousePressed || true);
      one.close();
      one =minim.loadFile("german.wav");
    
    }
    
  • edited January 2016 Answer ✓
    • When program enters mousePressed(), it is assumed that mousePressed is already true.
    • Same for mouseReleased(). But this time it is false. No need for those if () blocks.
    • Also favor loading all resources within setup() once only.
    • Seems like you haven't followed any of my advises? :-?


    void mousePressed() {
      one.loop();
    }
    
    void mouseReleased() {
      one.pause();
      one.rewind();
    }
    
Sign In or Register to comment.