Question

Here is what I have:

2 buttons

2 sounds

when button 1 press I want sound 1 played when button 2 pressed i want only sound2 played.

My code:

import ddf.minim.*;

RadioButtons r;
boolean showGUI = false;

Minim minim;
AudioPlayer player_1;
AudioPlayer player_2;

PImage img, img2;  
PShape img1;

void setup() {
  size(1024,735);
  String[] radioNames = {"Button1", "Button2"};
  r = new RadioButtons(radioNames.length, 20,700,50,30, HORIZONTAL);
  r.setNames(radioNames); 
  img = loadImage("img.jpg");
  img1 = loadImage("img1.png");
  img2 = loadShape("img1.svg");

  minim = new Minim(this);
  //sound1
  player_1 = minim.loadFile("sound1.wav");
  //sound2
  player_2 = minim.loadFile("soun2d2.wav");
}

void draw() {
  //background(0);
  //println (mouseX +"," + mouseY);
  // Draw the image to the screen at coordinate (0,0)
  //sound1
  image(img,0,0);
  if(r.get() == 0)
    shape(img1,695,106);
    if(mousePressed){
    if(mouseX>695 && mouseX <695+190 && mouseY>106 && mouseY <106+180){
    fill(0,0,0,0);
    image(img2,300,150);
    player_1.cue(0);
    player_1.play(); 
}
}
  //sound2
  if(r.get() == 1)
    shape(img1,695,106);
    if(mousePressed){
    if(mouseX>695 && mouseX <695+190 && mouseY>106 && mouseY <106+180){
    fill(0,0,0,0);
    image(img2,300,150);
    player_2.cue(0);
    player_2.play(); 
    }
    }
    if(showGUI)
    {
       r.display();
    }      
}    
void mouseReleased()
{
  if(showGUI){
    if(mouseY> height-60)
      r.mouseReleased();
    else
      showGUI = false;
  }
  else{
    showGUI = true;
  }  
}

At the moment both sounds play at the same time.

What am I missing?

Was it helpful?

Solution 2

UPDATE::

I Solved this by

Adding IF & ELSE IF statements.

EXAMPLE BELOW.

void draw() {
 //background(0);
  //println (mouseX +"," + mouseY);
  // Draw the image to the screen at coordinate (0,0)
  //sound1
  image(img,0,0);
  if(r.get() == 0)
    {
  code...
}
}
}
  //sound2
  else if(r.get() == 1)
    {
    code...
    }
    }
    }
    if(showGUI)
    {
       r.display();
        }      
    }    

OTHER TIPS

You have to stop the current sample before you start the other one.
If you want to play player_2, you have to call:

player_1.pause();
player_1.rewind();

before you call player_2.play(); and the other way around.

I made this very stupid example, is it any help? It is stupid, specially the button class and event handling, but as I don't know which RadioButtons you are using I putted those lines together... maybe it can inspire you.

import ddf.minim.*;

PVector pOne, pTwo;
Button one, two;

Minim minim;
AudioPlayer player_1;
AudioPlayer player_2;

void setup() {
  size(300,300);
  pOne = new PVector(50, 150);
  pTwo = new PVector(150, 150);
  one = new Button (pOne, "one");
  two = new Button (pTwo, "two");
  minim = new Minim(this);
  //sound1
  player_2 = minim.loadFile("down_in_the_hole.mp3");
  //sound2
  player_1 = minim.loadFile("emotional_rescue.mp3");
}

void draw() {
  background(220);
  one.display();
  one.update();
  two.display();
  two.update();

  if(one.pressed){
  player_2.pause();
  player_1.rewind();
  player_1.play();
  ellipse(30,30,10,20);
 // two.pressed = false;
  }


  if(two.pressed){
  player_1.pause();
  player_2.rewind();
  player_2.play();
  ellipse(130,30,10,20);
  //one.pressed = false;
  }


}

void mouseReleased(){
  one.pressed = false;
  two.pressed = false;
}

class Button{

  String name;
  PVector pos;
  boolean pressed = false;
  int xsz = 40;
  int ysz = 20;

  Button(PVector _pos, String _name ){
    pos = _pos;
    name = _name;
  }

  void display(){
    color c = isOver()? 255:120;
    fill(c);
    rect(pos.x, pos.y, xsz, ysz);
    fill(0);
    text(name, pos.x+5, pos.y +ysz/2);
  }

  void update(){
    if(isOver() && mousePressed)
    pressed = true;
  }

  boolean isOver(){
    return mouseX > pos.x && mouseX < pos.x + xsz &&
           mouseY > pos.y && mouseY < pos.y + ysz; 
  }

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top