We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I'm using the 'scrollableList' from the controlP5 library. I wish to simply play a random sound file each time I click an element on the scrollable list, without pausing.
I have tried a few things. I am having limited success with some code from @akenaton that I found in this thread: https://forum.processing.org/two/discussion/8949/how-do-i-play-a-random-audio-sample
I merged that code with the scrollable list. So far, I'm only managing to pause the audio on clicking the menu. I am probably missing something obvious.
As ever, any tips much appreciated :)
import ddf.minim.*;
import controlP5.*;
import java.util.*;
AudioPlayer player;
Minim minim;
ControlP5 cp5;
boolean playeurInit = false;// que leplayer n'est pas lancé
boolean stop = true;
String[] table = {"train1.wav", "train2.wav"};
int randomWav;
String wav;
void setup() {
size(400, 400);
minim = new Minim(this);
int randomWav = int(random(table.length));
String son = table[randomWav];
//println(son);
player = minim.loadFile(son);
player.pause();
cp5 = new ControlP5(this);
List l = Arrays.asList("a", "b", "c", "d", "e", "f", "g", "h");
cp5.addScrollableList("dropdown").setPosition(10, 10).setSize(200, 100).setBarHeight(20).setItemHeight(20).addItems(l);
}
void draw() {
background(240);
if (!stop) {
if (player.isPlaying() == false) {
randomWav = int(random(table.length));
println(randomWav);
wav = table[randomWav];
player = minim.loadFile(wav);
player.play();
player.loop();//you can change that!
playeurInit = true;
}
}
}
void dropdown(int n) {
if(stop == true)
{
stop = false;
}else{
stop = true;
player.play();
if (player.isPlaying() == true )
{
player.pause();
//player.play();
}
}
CColor c = new CColor();
c.setBackground(color(255,0,0));
cp5.get(ScrollableList.class, "dropdown").getItem(n).put("color", c);
}
void stop(){
player.close();
minim.stop();
super.stop();
}
Answers
Check the code below. No need to call play() and loop(). You need only the former.
Kf
Thanks, @kfrajer. I am running the code now but it does not randomise each of my two wav files?
I simplified my scenario to just using a keypress 'L' to randomise. But in doing so I run into more trouble. The previous sound file seems to always keep playing when the new randomly chosen one starts. So, if I press 'L' many times many samples play once. I wish for each new sample to stop the previous.
Here's the gist of that code:
I tested the code with two very short "noises". They are random. Maybe you didn't try enough times? J/k... it plays only the first one or the second one? Just to have an idea, how long are each file when you play them?
Kf
These are 3minute long atmos files. I also need one to stop right when the next is triggered. I tried many times yes, they don't randomise and I'm sure I have correct samples loaded.
Modified from: https://forum.processing.org/two/discussion/21316/how-do-i-play-the-same-soundfile-multiple-times-simultaneously
Tested.
Kf
Keywords: kf_keyword minim multiple sound files
Excellent stuff. Thank you very much, @krajer. And it fits into the scrollable list code nicely too :)