Audio Not Playing With if Statements (Minim)

Hi all!

I can’t seem to get sound files to play with this code here. I want different sound files to play when the user hits a certain key. I’m working on a project currently, here’s the code below!



//Audio and Variables (beginning non-method things)
import ddf.minim.*;

Minim minim;
AudioPlayer bearSound, cowSound, sheepSound, pigSound, dogSound, horseSound, frogSound, lionSound, wolfSound, catSound, goatSound, fishSound;


float fs, r, g, b, sp;
int fx, fy, h;
int count = 0;
boolean load = false;

void setup(){
  //General Information
  size(876, 665);
  
 r = 200;
 g = 200;
 b = 281;
 fx = 20;
 fy = 20;
 fs = 194;
 sp = 20;
 h = 9;
 
 //Sound Stuff! 
 minim = new Minim(this);
 bearSound = minim.loadFile("bearsound.mp3");
 cowSound = minim.loadFile("cowsound.mp3");
 sheepSound = minim.loadFile("sheepsound.mp3");
 pigSound = minim.loadFile("pigsound.mp3");
 dogSound = minim.loadFile("dogsound.mp3");
 horseSound = minim.loadFile("horsesound.mp3");
 frogSound = minim.loadFile("frogsound.mp3");
 lionSound = minim.loadFile("lionsound.mp3");
 wolfSound = minim.loadFile("wolfsound.mp3");
 
 
 
  if(keyPressed){
       if(key == 'b'){
        bearSound.play(); 
        bearSound.rewind();
       }
        if(key == 'c'){
         cowSound.play(); 
         cowSound.rewind();
        }
        if(key == 's'){
         sheepSound.play(); 
         sheepSound.rewind();
        }
        if(key == 'p'){
         pigSound.play();
         pigSound.rewind();
        }
        if(key == 'd'){
          dogSound.play();
          dogSound.rewind();
        }
        
      }
      
      
 
 
 
 

 
}


void draw(){
  
     //Image Loadups
   PImage bear, cow, sheep, pig, dog, cat, frog, lion, wolf, horse, goat, fish;
 bear = loadImage ("bear.jpg");
 cow = loadImage ("cow.jpg");
 sheep = loadImage ("sheep.jpg");
 pig = loadImage ("pig.jpg");
 dog = loadImage ("dog.jpg");
 cat = loadImage ("cat.jpeg");
 frog = loadImage ("frog.jpg");
 lion = loadImage ("lion.jpg");
 wolf = loadImage ("wolf.jpg");
 horse = loadImage ("horse.jpg");
 goat = loadImage ("goat.jpg");
 fish = loadImage ("fish.jpg");
 
  //Image Square Figures
  bear.resize(PApplet.parseInt(fs), PApplet.parseInt(fs));
  cow.resize(PApplet.parseInt(fs), PApplet.parseInt(fs));
  sheep.resize(PApplet.parseInt(fs), PApplet.parseInt(fs));
  dog.resize(PApplet.parseInt(fs), PApplet.parseInt(fs));
  cat.resize(PApplet.parseInt(fs), PApplet.parseInt(fs));
  lion.resize(PApplet.parseInt(fs), PApplet.parseInt(fs));
  goat.resize(PApplet.parseInt(fs), PApplet.parseInt(fs));
  wolf.resize(PApplet.parseInt(fs), PApplet.parseInt(fs));
  frog.resize(PApplet.parseInt(fs), PApplet.parseInt(fs));
  horse.resize(PApplet.parseInt(fs), PApplet.parseInt(fs));
  fish.resize(PApplet.parseInt(fs), PApplet.parseInt(fs));
  pig.resize(PApplet.parseInt(fs), PApplet.parseInt(fs));
 

  
  //Startup Screen
  background(r, g, b);
  triangle(mouseX, mouseY, 20, 20, 20, 20);
  textAlign(CENTER);
  textSize(40);
  text("learning animal sounds!", width/2, height/3);
  noStroke();
  ellipseMode(CENTER);
  ellipse(width/2, (height/3)+80, 500, 30);
  text("press 'v' to start",width/2, (height/2)+80);
  
   if(key == 'v'){
     load = true;
   }
   
   if(load == true){
     rectMode(CORNER); 
     fill(r, g, b);
    rect(0, 0, width, height);  
      //Drawing the Images
  image (bear, fx, fy);
  image (cow, fx+fs+sp, fy);
  image (sheep, fx+fs+fs+sp+sp, fy);
  image (pig, fx+fs+fs+fs+sp+sp+sp, fy);
  image (dog, fx, fy+fs+sp);
  image (cat, fx+fs+sp, fy+fs+fs+sp+sp);
  image (frog, fx+fs+fs+sp+sp, fy+fs+sp);
  image (lion, fx+fs+fs+fs+20+20+20, fy+fs+sp);
  image (wolf, fx, fy+fs+fs+sp+sp);
  image (horse, fx+fs+sp, fy+fs+sp);
  image (goat, fx+fs+fs+sp+sp, fy+fs+fs+sp+sp);
  image (fish, fx+fs+fs+fs+sp+sp+sp, fy+fs+fs+sp+sp);
  
  //bear highlight
  if(mouseX >= 20 && mouseX <= 194+20 && mouseY >= 20 && mouseY <= 194+20){
   fill(255, 150);
   strokeWeight(h);
   stroke(255);
   rect(fx, fy, fs, fs);
   fill(0);
   text("Bear",120, 120);
  }
   
   //cow highlight
     if(mouseX >= 194+20+20 && mouseX <= 194+20+20+194+20 && mouseY >= 20 && mouseY <= 194+20){
   fill(255, 150);
   strokeWeight(h);
   stroke(255);
   rect(fx+fs+sp, fy, fs, fs);
   fill(0);
   text("Cow", 332, 120);
  }
  
  //sheep highlight
  if(mouseX >= 194+194+20+20+20 && mouseX <= 194+194+194+20+20+20 && mouseY >= 20 && mouseY <= 194+20){
    fill(255, 150);
    strokeWeight(h);
    stroke(255);
    rect(fx+fs+fs+sp+sp, fy, fs, fs);
    fill(0);
    text("Sheep", 544, 120);
  }
  
  //pig highlight
  if(mouseX >= 194+194+194+20+20+20+20 && mouseX <= 194+194+194+194+20+20+20+20 && mouseY >= 20 && mouseY <= 194+20){
   fill(255, 150);
   strokeWeight(h);
   stroke(255);
   rect(fx+fs+fs+fs+sp+sp+sp, fy, fs, fs);
   fill(0);
   text("Pig", 756, 120);
  }
  
   //dog highlight
    if(mouseX >= 20 && mouseX <= 194+20 && mouseY >= 20+194+20 && mouseY <= 194+20+20+194){
   fill(255, 150);
   strokeWeight(h);
   stroke(255);
   rect(fx, fy+fs+sp, fs, fs);
   fill(0);
   text("Dog", 120, 335);
    }
    
    //horse highlight
  if(mouseX >= 194+20+20 && mouseX <= 194+20+20+194+20 && mouseY >= 20+194+20 && mouseY <= 194+20+20+194){
   fill(255, 150);
   strokeWeight(h);
   stroke(255);
   rect(fx+fs+sp, fy+fs+sp, fs, fs);
   fill(0);
   text("Horse", 332, 335);
  }
  
  //frog highlight
    if(mouseX >= 194+194+20+20+20 && mouseX <= 194+194+194+20+20+20 && mouseY >= 20+194+20 && mouseY <= 194+20+20+194){
    fill(255, 150);
    strokeWeight(h);
    stroke(255);
    rect(fx+fs+fs+sp+sp, fy+fs+sp, fs, fs);
    fill(0);
    text("Frog", 544, 335);
  }
  
  //lion highlight
    if(mouseX >= 194+194+194+20+20+20+20 && mouseX <= 194+194+194+194+20+20+20+20 && mouseY >= 20+194+20 && mouseY <= 194+20+20+194){
   fill(255, 150);
   strokeWeight(h);
   stroke(255);
   rect(fx+fs+fs+fs+sp+sp+sp, fy+fs+sp, fs, fs);
   fill(0);
   text("Lion", 756, 335);
  }
  
  //wolf highlight
    if(mouseX >= 20 && mouseX <= 194+20 && mouseY >= 20+20+20+194+194 && mouseY <= 194+20+20+20+194+194){
   fill(255, 150);
   strokeWeight(h);
   stroke(255);
   rect(fx, fy+fs+fs+sp+sp, fs, fs);
   fill(0);
   text("Wolf", 120, 555);
  }
  
  //cat highlight
  if(mouseX >= 194+20+20 && mouseX <= 194+20+20+194+20 && mouseY >= 20+20+20+194+194 && mouseY <= 194+20+20+194+20+194){
   fill(255, 150);
   strokeWeight(h);
   stroke(255);
   rect(fx+fs+sp, fy+fs+fs+sp+sp, fs, fs);
   fill(0);
   text("Cat", 332, 555);
  }
  
  //goat highlight
    if(mouseX >= 194+194+20+20+20 && mouseX <= 194+194+194+20+20+20 && mouseY >= 20+20+20+194+194 && mouseY <= 194+20+20+194+20+194){
    fill(255, 150);
    strokeWeight(h);
    stroke(255);
    rect(fx+fs+fs+sp+sp, fy+fs+fs+sp+sp, fs, fs);
    fill(0);
    text("Goat", 544, 555);
  }
  
  //fish highlight
    if(mouseX >= 194+194+194+20+20+20+20 && mouseX <= 194+194+194+194+20+20+20+20 && mouseY >= 20+20+20+194+194 && mouseY <= 194+20+20+194+20+194){
   fill(255, 150);
   strokeWeight(h);
   stroke(255);
   rect(fx+fs+fs+fs+sp+sp+sp, fy+fs+sp+fs+sp, fs, fs);
   fill(0);
   text("Fish", 756, 555);
      }
   
}

}
1 Like

Please read some textual tutorials (the first 3)

Read especially the section “Hello mouse” here: https://www.processing.org/tutorials/overview/

You seem to have mixed up setup() and draw():

  • Keys won’t be registered in setup(), they belong in draw()

  • Loading images and other things to do only once belong into setup()

Warmest Regards,

Chrisir

1 Like

Thank you for that! The key functions are now all in the draw method!

I still can’t get the audio to play though- I keep getting mp3 ID3 tag errors, like apic and priv. Would removing the tags from the mp3 files help?

The id3 errors seem to be hints only, not hard errors; my mp3 files play with the error message too.

Please show your entire current code so we can look for errors

here is an example


import ddf.minim.*;
Minim minim = null;
AudioPlayer groove = null;

void setup() {
  size(512, 200, P3D);
  background(0);

  minim = new Minim(this);
  groove = minim.loadFile("groove.mp3", 2048);
  if (groove==null) {
    println ("failed to load mp3-file."); 
    exit();  // leave 
  } 
}

void draw() {
  background(0);

  if ( ! groove.isPlaying() )    // The ! means not 
    text("Hit Space Bar",       // show text 
      111, 111);
}

void keyPressed() {
  if (key==' ') {
    groove.loop();   // start playing 
  }
}
//
1 Like