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

**URL:** <https://discourse.processing.org/t/how-to-know-when-a-sound-file-has-been-read-in-its-entirety/9873>\
**Category:** Electronics (Arduino, etc.)\
**Created:** [April 2, 2019, 1:46pm UTC](https://discourse.processing.org/t/how-to-know-when-a-sound-file-has-been-read-in-its-entirety/9873 "2019-04-02T13:46:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mimal](https://avatars.discourse-cdn.com/v4/letter/m/ac8455/32.png) [@Mimal](https://discourse.processing.org/u/Mimal)\
**Post date:** [April 2, 2019, 1:46pm UTC](https://discourse.processing.org/t/how-to-know-when-a-sound-file-has-been-read-in-its-entirety/9873/1 "2019-04-02T13:46:00Z")

</div>

Hi,

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.

```auto
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);
  size(200,200);
  port = new Serial(this, Serial.list()[0], 9600);
  port.bufferUntil('\n');
  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() {
  radio();
  delay(100);
}

void radio() {
  println("canal " + value);
  if (value==1) {
    bruitage.mute();
    radio1.unmute();
    radio1.play();
  }
  if (!radio1.isPlaying() && value==1) {
    radio1.rewind();
    radio1.play();
  }
  if (value==2) {
    radio1.mute();
    bruitage.unmute();
    bruitage.play();
  }
  if (!bruitage.isPlaying() && value==2) {
    bruitage.rewind();
    bruitage.play();
  //...
}

void serialEvent(Serial port){
    String serialStr = port.readStringUntil('\n');
    serialStr=trim(serialStr);
    int values[]=int(split(serialStr,','));
    if(values.length==1){
      value=values[0];
    }
}

```

Could someone please help me?

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [April 2, 2019, 2:29pm UTC](https://discourse.processing.org/t/how-to-know-when-a-sound-file-has-been-read-in-its-entirety/9873/2 "2019-04-02T14:29:06Z")

</div>

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 /  
> loop  
> loopNum

can use

> .loop() instead of .play()

also this

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

might help

more:  
[http://code.compartmental.net/minim/audioplayer\_method\_loop.html](http://code.compartmental.net/minim/audioplayer_method_loop.html)

---

<div class="post-metadata">

**Author:** ![Mimal](https://avatars.discourse-cdn.com/v4/letter/m/ac8455/32.png) [@Mimal](https://discourse.processing.org/u/Mimal)\
**Post date:** [April 8, 2019, 8:40am UTC](https://discourse.processing.org/t/how-to-know-when-a-sound-file-has-been-read-in-its-entirety/9873/3 "2019-04-08T08:40:12Z")

</div>

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 ?

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [April 8, 2019, 8:55am UTC](https://discourse.processing.org/t/how-to-know-when-a-sound-file-has-been-read-in-its-entirety/9873/4 "2019-04-08T08:55:58Z")

</div>

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

```auto
void keyPressed() { // or any other event...
  toggle_groove();
}

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

```

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

---

<div class="post-metadata">

**Author:** ![Mimal](https://avatars.discourse-cdn.com/v4/letter/m/ac8455/32.png) [@Mimal](https://discourse.processing.org/u/Mimal)\
**Post date:** [April 8, 2019, 9:02am UTC](https://discourse.processing.org/t/how-to-know-when-a-sound-file-has-been-read-in-its-entirety/9873/5 "2019-04-08T09:02:10Z")

</div>

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

```auto
 if (radio1.isPlaying() && value==1) {
   bruitage.mute();
   radio1.unmute();
 }

```

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
