How to know when a sound file has been read in its entirety?


Here my program that retrieves a value from the Arduino card and plays a sound based on that value.
The value varies according to the position of a potentiometer: an Arduino program transforms the voltage across the potentiometer into 3 digital values: 1, 2 and 3.
The Processing program must play sound 1 when the recovered value is 1, play sound 2 when the recovered value is 2 and play sound 3 when the value is 3, etc.
It works but I want that when you switch from sound 1 to sound 2 (or from sound 2 to 3, etc.), sound 1 is not stopped but only muted. Once the sound has been played in its entirety, it must be automatically restarted.
I don’t know how to do it, I think I have to know when the sound is done, but I didn’t find a function like that.

import processing.serial.*;
import ddf.minim.*;

Serial port;
Minim minim;
AudioPlayer radio1;
AudioPlayer radio2;
AudioPlayer radio3;
AudioPlayer bruitage;

int value=0;

void setup() {
  background(0, 60, 60);
  port = new Serial(this, Serial.list()[0], 9600);
  minim = new Minim(this);
  radio1 = minim.loadFile("radio1.mp3");
  radio2 = minim.loadFile("radio2.mp3");
  radio3 = minim.loadFile("radio3.mp3");
  bruitage = minim.loadFile("bruitage.mp3");

void draw() {

void radio() {
  println("canal " + value);
  if (value==1) {
  if (!radio1.isPlaying() && value==1) {
  if (value==2) {
  if (!bruitage.isPlaying() && value==2) {

void serialEvent(Serial port){
    String serialStr = port.readStringUntil('\n');
    int values[]=int(split(serialStr,','));

Could someone please help me?

1 Like

starting with the arduino part again
( what actually was solved as i remember )
is only confusing, @GoToLoop
your question is about MINIM library

did you check the MINIM examples?

/ Files / Contributed Libraries / Minim / AudioPlayer /

can use

.loop() instead of .play()

also this

text(" Is playing: " + groove.isPlaying()
+ ", Is looping: " + groove.isLooping(), 5, 15);

might help



Sorry for the late answer.

I tried to replace play() with loop() method and and it seems to work for a change in value on the potentiometer because the sound change when i turn the potentiometer one time. The text shows that the first song keeps playing and it’s muted. It’s good, but, when I turn the potentiometer a second time, the second song keeps playing in a loop, whatever the value of the potentiometer.

Could you help me please ?

the basic form of operating ( start stop ) songs in MINIM is

void keyPressed() {   // or any other event...

void toggle_groove() {
  if ( groove.isPlaying() )  groove.pause();
  else                       groove.loop();

but not sure if that helps in your 4 song logic??

The answer was obvious, I need to put this second loop for each sound :

 if (radio1.isPlaying() && value==1) {

I know there’s probably a better way to do this but, it’s worked so it’s okay ahah
Thank you for you big help