We are about to switch to a new forum software. Until then we have removed the registration on this forum.
In my simple picture dictionary code cited below, I add the last line in setup() [line 29], i.e. preload the 1st sound clip, or else it doesn't play on 1st instance of play().
I guess this is not the best solution. Any advice?
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 player;
PImage image, imageSprite;
int sX, sY, eX, eY ;
XML fruitList ;
XML[] fruits ;
int click;
String name, thisPage, startMessage;
void setup() {
size(600, 400) ;
minim = new Minim(this);
click = 0 ;
name ="";
thisPage = "";
image = loadImage("fruits.jpg") ;
fruitList = loadXML("fruitList.xml") ;
fruits = fruitList.getChildren("fruit") ;
startMessage = "In this lesson, you will learn " + fruits.length + " words.\nLeft-click to start & go next. \nRight-click to go back." ;
player = minim.loadFile("0.mp3", 2048); // to preload the initial sound clip
}
void draw() {
if (click == 0) {
background(255) ;
textSize(20);
fill(0);
text(startMessage, width/2 - textWidth(startMessage)/2, height/2 - 40);
} else {
background(255) ;
name = fruits[click-1].getChild("name").getContent();
fill(0);
textSize(40);
text(name, width/2 - textWidth(name)/2, 60);
sX = int(fruits[click-1].getChild("get/sX").getContent());
sY = int(fruits[click-1].getChild("get/sY").getContent());
eX = int(fruits[click-1].getChild("get/eX").getContent());
eY = int(fruits[click-1].getChild("get/eY").getContent());
imageSprite = image.get(sX, sY, eX - sX, eY - sY);
image(imageSprite, width/2 - imageSprite.width/2, height/2 - imageSprite.height/2) ;
thisPage = click + "/" + fruits.length ;
fill(127);
textSize(20);
text(thisPage, width - textWidth(thisPage), height -25);
}
}
void mouseClicked () {
if (mouseButton == LEFT) {
click++ ;
} else if (mouseButton == RIGHT) {
click-- ;
}
click = constrain(click, 1, fruits.length);
player = minim.loadFile(click-1 + ".mp3", 2048); // 1024, 2048
player.setGain(-20);
player.play();
}
Answers
Without line 29, your mouseClicked() has no effect?
Kf
Dear kfrajer,
Not quite.
Say, I comment out line 29...
When I run the program & give the 1st left click, mouseClicked() makes the value of "click" to 1, so at line 68+ "0.mp3" should be loaded & played, but there is no sound.
Then, when I give another click (the value of "click" is now 2), "1.mp3" is played alright
Then, when I give a right click to revert the value of "click" to 1, "0.mp3" IS played.
I can reproduce it as I don't have the files. Something that could make a difference. At line 68:
player = minim.loadFile( (click-1) + ".mp3", 2048);
Not sure if it will make a difference. Give it a try.
I also tested it with this small change in your last function:
Kf
You can enable the setGain() line. I tested it and it works.
Kf
Thanks for your suggestion, kfrajer. I tried it, but the result is the same.
I've realized that my PC's power option was set to "eco", so I tried changing it to "high performance", and then it works.
With the "eco" mode (or on low performance PCs), the preloading at line 29 still seems to be a way of workaround.