Childrens game

edited August 2016 in Library Questions

Hi I've made this small game, when a letter is pressed its displayed on screen as an image and a sound plays. When I run the code and press a letter it shows the letter and plays the sound, but when I press the letter again it displays the letter but doesn't play the sound. I've tried using .rewind(); and what happens after that it first time I press a letter it plays the sound and displays the letter, and if I press the same letter again it displays the letter and when i release the key it plays the sound :S.

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

Minim minim;
AudioPlayer ha;
AudioPlayer sh;
AudioPlayer no;
AudioPlayer ra;
AudioPlayer ba;
AudioPlayer lh;

PImage[] letters = new PImage[6];

void setup() {
  size(400, 400);
  background(0);

  minim = new Minim(this);
  ha = minim.loadFile("0.mp3");
  sh = minim.loadFile("1.mp3");
  no = minim.loadFile("2.mp3");
  ra = minim.loadFile("3.mp3");
  ba = minim.loadFile("4.mp3");
  lh = minim.loadFile("5.mp3");

  imageMode(CENTER);

  for (int i = 0; i<letters.length; i++) {
    letters[i] = loadImage(+i+".jpg");
  }
}
void draw() {
  background(0);
  if ((keyPressed == true)&&(key == 'h')) {
    image(letters[0], width/2, height/2, 200, 200);   
    ha.play();
  }
  if ((keyPressed == true)&&(key == 's')) {
    image(letters[1], width/2, height/2, 200, 200);
    sh.play();
  }
  if ((keyPressed == true)&&(key == 'n')) {
    image(letters[2], width/2, height/2, 200, 200);
    no.play();
  }
  if ((keyPressed == true)&&(key == 'r')) {
    image(letters[3], width/2, height/2, 200, 200);
    ra.play();
  }
  if ((keyPressed == true)&&(key == 'b')) {
    image(letters[4], width/2, height/2, 200, 200);
    ba.play();
  }
  if ((keyPressed == true)&&(key == 'l')) {
    image(letters[5], width/2, height/2, 200, 200);
    lh.play();
  }
}

Answers

  • I would suggest trying a small modification in your draw method. Assign the variable ha right before you call play like this:

        ha = minim.loadFile("0.mp3");
        ha.play();
    

    Run your program and see if thist improves your case.

    Kf

  • @kfrajer no it didn't improve it. it keeps loading in the loop. i tried putting an else {ha.rewind();} after the if but then it keeps rewinding as it loops.

  • never load files in draw. it's an expensive operation and is likely to cause delays.

    you have 6 blocks all starting with

    if ((keyPressed == true)&&
    

    you could simplify that by moving if (keyPressed) so it's called once around the whole lot.

    however, i'd use keyPressed(), the method, instead. see the reference for the differences and why you'd choose one over the other.

    you have an array for images, why not an array for the audio players as well?

    none of this fixes your problem though 8)

  • @koogs i got confused putting the audio files into an array, could you help me thanks

  • Answer ✓

    This is untested code. Give it a try.

    Kf

    import ddf.minim.spi.*;
    import ddf.minim.signals.*;
    import ddf.minim.*;
    import ddf.minim.analysis.*;
    import ddf.minim.ugens.*;
    import ddf.minim.effects.*;
    
    
    PImage[] letters = new PImage[6];
    AudioPlayer audio[] = new AudioPlayer[6];
    boolean playingFlag[] = new boolean[6];
    
    void setup() {
      size(400, 400);
      background(0);
      imageMode(CENTER);
    
      for (int i = 0; i<letters.length; i++) {
        letters[i] = loadImage(+i+".jpg");
        audio[i]=minim.loadfile(i+".mp3");
        playingFlag[i]=false;
      }
    
      background(0);
    }
    
    
    void draw() {
    
      for(j=0;j<letters.length;j++){
        if(playingFlag[j]==true){
           background(0);
           image(letters[j], width/2, height/2, 200, 200);  
           if(audio[j]==null)
             audio[j]=minim.loadfile(j+".mp3");
           audio[j].play();
           playingFlag[j]=false;
        }
      }
    }
    
    void keyPressed(){
    
      switch(key){
        case 'b':
          playingFlag[4]=true;
        break;
        case 'h':
          playingFlag[0]=true;
        break;
        case 'l':
          playingFlag[5]=true;
        break;
        case 'n':
          playingFlag[2]=true;
        break;
        case 'r':
          playingFlag[3]=true;
        break;
        case 's':
          playingFlag[1]=true;
        break;
        default:
      } 
    }
    
  • I don't think you need minim at all in your code. This works for me

    Kf

    import processing.sound.*;
    SoundFile file;
    
    void setup() {
      size(640, 360);
      background(255);
    
      // Load a soundfile from the /data folder of the sketch and play it back   
      file = new SoundFile(this,"C:\\Users\\C\\Downloads\\Crickets.mp3");
      file.play();
    }     
    
    void draw() {; }
    
Sign In or Register to comment.