Loading...
Logo
Processing Forum
First day using sound and processing. Im trying out Minim because it seems the easiest to use. My sketch loads 5 images and allows you to click the right and left edge to cycle the images. if you click the center of the sketch it triggers the sound file to play.
 
however, the sound file stops a couple seconds early. if you click again it starts from where it left off but doesnt compete. the audio stops playing but will never play the complete track. keep clicking and the sound gets shorter and shorter but never finishes. always starting from where the audio cut out the time before.
 
im using processing 2.0b3.
and minim 2.02
 
any help would be great
 
thank you.
 
code:
Copy code

  1.  import ddf.minim.*;
     import ddf.minim.signals.*;//dont need yet
     import ddf.minim.analysis.*;//dont need yet
     import ddf.minim.effects.*;//dont need yet
  2.   //minim
      Minim minim;
      AudioPlayer song;

     
      //a variable to contain our array number
      //set to zero because that is the default
      //value of an array
      int imgVar = 0;

  3.   //our main array var
      int i;

  4.   //name our arrays
      String[] imgUrls;
      PImage[] images;

  5. void setup(){
      size(150, 100);
      background(0);
      loadImg();
     
      //minim
      minim = new Minim(this);

     
      // this loads mysong.wav from the data folder
      song = minim.loadFile("owl.wav");

     
  6. }//setup

  7. void draw(){
      drawRecR();
      drawRecL();
  8.   //draw current image to screen
      image(images[i], 25, 0 );
    }//draw

  9. void mousePressed(){
     
      //if the mouse is over left button
      if (mouseX >= 0 && mouseX <= 25){
       println("left button");
       i--;
          if ( i < 0){
            //must subtract from array length to
            //account for 0 being counted
            i = images.length - 1;
          }//if
      }//ifML
     
      //if the mouse is over the center image
      else if (mouseX >= 26 && mouseX <= 124){
        println("center button");
        //play the sound
        play();

      }//ifMC
     
      //if the mouse is over the right button
      else if (mouseX >= 125 && mouseX <= 150){
        println("right button");
        i++;
          if ( i >= (imgUrls.length)){
            i = 0;
          }//if
      }//ifML
       println(i);
       println(mouseX);
    }//mouse pressed

  10. //push list of images into array
    void loadImg(){
      imgUrls= loadStrings("list.txt");
      images = new PImage[imgUrls.length];
         for(int i = 0; i<imgUrls.length;i++){
            images[i] = loadImage(imgUrls[i]);
              println(i);
         }//fori
    }//loadImg

  11. ////draws rectanges to visualize pre and next buttons
    void drawRecL(){
      fill(50);
      rect(0,0,25,100);
    }//drawRec
    void drawRecR(){
      fill(25);
      rect(125,0,25,100);
    }//drawRec

  12. //control when the sound file is played
    void play(){
      song.play();
    }//play
  13. //control when the sound file is stoped
    void stop(){
      song.close();
      minim.stop();
     
      super.stop();
    }//stop
 ********************************************************************************************************************
update: fixed it myself
 
used the example from learningprocessing.com
 
 
had to switch from calling the audioPlayer to audioSnippet.
added logic to my play method
1. rewind makes sure the file starts from beginning
2. the if statement makes sure it doesnt play if it is already playing
Copy code

  1.  import ddf.minim.*;
     import ddf.minim.signals.*;//dont need yet
     import ddf.minim.analysis.*;//dont need yet
     import ddf.minim.effects.*;//dont need yet
  2.   //minim
      Minim minim;
      AudioSnippet song;// changed this
     
      //a variable to contain our array number
      //set to zero because that is the default
      //value of an array
      int imgVar = 0;
      //our main array var
      int i;
      //name our arrays
      String[] imgUrls;
      PImage[] images;

  3. void setup(){
      size(150, 100);
      background(0);
      loadImg();
     
      //minim
      minim = new Minim(this);
     
      // this loads mysong.wav from the data folder
      song = minim.loadSnippet("owl.wav");
      //song.play();
  4. }//setup

  5. void draw(){
      drawRecR();
      drawRecL();
      // println(mouseX);
      image(images[i], 25, 0 );
    }//draw

  6. void mousePressed(){
     
      //if the mouse is over left button
      if (mouseX >= 0 && mouseX <= 25){
       println("left button");
       i--;
          if ( i < 0){
            //must subtract from array length to
            //account for 0 being counted
            i = images.length - 1;
          }//if
      }//ifML
     
      //if the mouse is over the center image
      else if (mouseX >= 26 && mouseX <= 124){
        println("center button");
        //play the sound
        play();
      }//ifMC
     
      //if the mouse is over the right button
      else if (mouseX >= 125 && mouseX <= 150){
        println("right button");
        i++;
          if ( i >= (imgUrls.length)){
            i = 0;
          }//if
      }//ifML
       println(i);
       println(mouseX);
    }//mouse pressed

  7. //push list of images into array
    void loadImg(){
      imgUrls= loadStrings("list.txt");
      images = new PImage[imgUrls.length];
         for(int i = 0; i<imgUrls.length;i++){
            images[i] = loadImage(imgUrls[i]);
              println(i);
         }//fori
    }//loadImg

  8. ////draws rectanges to visualize pre and next buttons
    void drawRecL(){
      fill(50);
      rect(0,0,25,100);
    }//drawRec
    void drawRecR(){
      fill(25);
      rect(125,0,25,100);
    }//drawRec

  9. //control when the sound file is played
    void play(){
      if (!song.isPlaying()){
        song.rewind();
        song.play();
      }

    }//play
  10. //control when the sound file is stoped
    void close(){
      song.close();
      minim.stop();
     
      super.stop();
    }//stop