Home>

A method to control ON/OFF with tact switch while looping mp3 sound source.

Error message
Using Arduino and processing, playing an mp3 file only while pressing the tact switch
I have done it, but how can I write it to keep the sound source looped? Yoro
Thank you very much.
Applicable source code
import ddf.minim. *;
import ddf.minim.analysis. *;
import ddf.minim.effects. *;
import ddf.minim.signals. *;
import ddf.minim.spi. *;
import ddf.minim.ugens. *;
import cc.arduino. *;// Arduino library
import processing.serial. *;// Serial communication library
Arduino arduino;
int pin2 = 2;// Input pin (digital)
//sound
Minim minim;
AudioPlayer song7;// Sound data storage variable declaration
void setup () {
  println (Arduino.list ());// Display a list of serial ports
  arduino = new Arduino (this, Arduino.list () [1], 57600);// Arduino connection settings
  arduino.pinMode (pin2, Arduino.INPUT);// I/O pin setting
    // setup
  minim = new Minim (this);// initialization
  // [first piece]
  song7 = minim.loadFile ("sample7.mp3");// load sample7.mp3
  song7.play ();// Play
}
void draw () {
  int value = arduino.digitalRead (pin2);// read pin state
  if (value == arduino.LOW) {// input is LOW (plays music when the button is pressed)
    background (255,0,0);
    song7.play ();
  }
  else if (value == arduino.HIGH) {// input is HIGH (pauses music when the button is not pressed)
    background (0,0,0);
    song7.pause ();
  }
}

//sound
void stop () {
  song7.close ();// End sound data
  minim.stop ();
  super.stop ();
}

I tried putting "sound7.loop ();" behind the play in setup and behind the pause of draw, but nothing happened.

  • Answer # 1

    Since there is no Arduino, when I tried to move the variable appropriately, it was as intended.

    void draw () {
      int value = arduino.digitalRead (pin2);
      if (value == arduino.LOW) {
        background (255, 0, 0);
        if (! song7.isPlaying ()) {
          song7.loop ();
        }
      } else if (value == arduino.HIGH) {
        background (0, 0, 0);
        song7.pause ();
      }
    }