[FIXED]hard to describe situation with Minim. audio clips not playing all the way through. when played again they start where they left off. what is going on?
in
Core Library Questions
•
1 year ago
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:
import ddf.minim.*;
import ddf.minim.signals.*;//dont need yet
import ddf.minim.analysis.*;//dont need yet
import ddf.minim.effects.*;//dont need yet- //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;
//our main array var
int i;
//name our arrays
String[] imgUrls;
PImage[] images;
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");
- }//setup
void draw(){
drawRecR();
drawRecL();- //draw current image to screen
image(images[i], 25, 0 );
}//draw
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
//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
////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
//control when the sound file is played
void play(){
song.play();
}//play- //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
import ddf.minim.*;
import ddf.minim.signals.*;//dont need yet
import ddf.minim.analysis.*;//dont need yet
import ddf.minim.effects.*;//dont need yet- //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;
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();- }//setup
void draw(){
drawRecR();
drawRecL();
// println(mouseX);
image(images[i], 25, 0 );
}//draw
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
//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
////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
//control when the sound file is played
void play(){
if (!song.isPlaying()){
song.rewind();
song.play();
}
}//play- //control when the sound file is stoped
void close(){
song.close();
minim.stop();
super.stop();
}//stop
2