How to get a sound to play when I click an object

edited November 2016 in Library Questions
import ddf.minim.*;
int rad = 30;                                   // Width of the shape
float xpos, ypos;                               // Starting position of shape    
float xspeed = 4.5;                             // Speed of the shape
float yspeed = 4.5;                             // Speed of the shape    
int xdirection = 1;                             // Left or Right
int ydirection = 1;                             // Top to Bottom
int score=0;                                     //Inital score
int lives=5;                                     //Number of lives you start with
boolean lost=false;                              //Have you lost yet?                  
int speed=1;   
PImage bg;
int y;
AudioPlayer player;
Minim minim;

void setup() 
{
  size(1297, 736);
  minim = new Minim(this);
  fill(0,0,0);
  noStroke();
  frameRate(60);
  ellipse(xpos, height/10,100,100);
  xpos = width/2;                               // Set the starting position of the shape
  ypos = height/2;
  bg = loadImage("stem.jpg");                   //Load Image (jpg,png,gif)
  player = minim.loadFile("Song.wav");          //Load Audio File (mp3,wav)
  player.loop();

}

void draw() 
{
  background(bg);

  xpos = xpos + ( xspeed * xdirection );         // Update the position of the shape
  ypos = ypos + ( yspeed * ydirection );


  if (xpos > width-rad || xpos < rad) {          // Test to see if the shape exceeds the boundaries of the screen
                                                 // If it does, reverse its direction by multiplying by -1
    xdirection *= -1; 
  }
  if (ypos > height-rad || ypos < rad) {
    ydirection *= -1;
  }


  ellipse(xpos, ypos, rad, rad);                    //Draw the shape
  text("score = "+score,10,10);                     //Print the score on the screen
  text("lives = "+lives,width-80,10);               //Print remaining lives
  if (lives<=0)                                     //Check to see if you lost
  {
    textSize(50);
    text("Click to Restart", 450,200);
    noLoop();                                       //Stop looping at the end of the draw function
    lost=true;
    textSize(13);
  }
}
void mousePressed()                                 //Runs whenever the mouse is pressed
{
  if (dist(mouseX, mouseY, xpos, ypos)<=rad)        //Did we hit the target?
  {
    score=score+speed;                              //Increase the speed
    speed=speed+1;                                  //Increase the Score
  }
  else                                              //We missed
  {
    if (speed<1)                                    //If speed is greater than 1 decrease the speed
    {
    speed=speed-1;
    }
    lives=lives-1;                                  //Take away one life
  }
  if (lost==true)                                   //If we lost the game, reset now and start over 
  {
    speed=1;                                        //Reset all variables to initial conditions
    lives=5;
    score=0;
    xpos=width/2;
    xdirection=1;
    lost=false;
    loop();                                         //Begin looping draw function again
  }
}

As shown above, i'm able to get a song to play in the background using the minim library, i have a spare 'ding' sound effect, which i would love to add to the click of my mouse when i click the ball, the only problem is i'm unsure on how to do so. So basically, everytime the ball is clicked it produces a 'ding' sound. Thankyou to everyone who can take the time to help me!

Tagged:

Answers

  • What you need to do is use another Audio player to play the sound. If you start getting into more than one sample then you need to implement something called a sound pool. This allows you to cache your samples in an array ready to use whenever you want to play a certain sound. This can be implemented in a number of ways but I implement it using a hashmap.

    
    class SoundWindowsController implements ISoundController {
      Minim minim;
      HashMap soundPoolMap;
      //The parent processing class is PApplet 
      SoundWindowsController(PApplet _this) {
        minim = new Minim(_this) ;
        soundPoolMap = new HashMap(enum_SoundFX.values().length);
        //Load the sounds
        for (enum_SoundFX sfx : enum_SoundFX.values()) {
          try {
            soundPoolMap.put(sfx, minim.loadFile(sfx+".mp3"));
          } 
          catch(Exception e) {
           println("CANNOT LOAD SOUND FILE "+ sfx+".mp3");
          }
        }
      } 
      public void play(enum_SoundFX _val) {
        try {
          soundPoolMap.get(_val).rewind();
          soundPoolMap.get(_val).play();
        } 
        catch(Exception e) {
          println("CANNOT LOAD SOUND : "+_val);
        }
      }
      public void stop() {
        minim.stop();
      }
    
      void free() {
        minim = null;
        soundPoolMap.clear();
        soundPoolMap = null;
      }
    }
    
    

    Note that I use inversion of control so I can have a compatible Android and Windows version but you can strip this out.

    My sounds are within a enum

    
    enum  enum_SoundFX {
      WASSON1, WASSON2, WASSON3, WASSON4 ;
    }
    

    So when the object is created the files are loaded automatically as they are named the same as the enum.

    To play a same I just pass the enum of the sound I want to play i.e.

     soundfx.play(enum_SoundFX.WASSON2);
    
  • Sorry. I am new to this forum and as of yet not sure how to pretty code. Guessing it is not the pre tag

  • BTW soundfx is an instance of SoundWindowsController i.e.
    ISoundController soundfx = new SoundAndroidController(this);

  • edited November 2016

    @jellyfish, Thanks for the response! Is there a way i can add a second sound using the minim library? Just call it something else and make it so under my voidmouseclicked it will register the sound as i click my ball?

This discussion has been closed.