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

Minim minim;
AudioPlayer player;

int lf = 10;    // Linefeed in ASCII
String myString = null;
Serial myPort;  // The serial port
int sensorValue = 0;

void setup() {
  
  printArray(Serial.list());
  // Open the port you are using at the rate you want:
  myPort = new Serial(this, Serial.list()[0], 9600);
  myPort.clear();
  // Throw out the first reading, in case we started reading 
  // in the middle of a string from the sender.
  myString = myPort.readStringUntil(lf);
  myString = null;
  // we pass this to Minim so that it can load files from the data directory
  minim = new Minim(this);
  // loadFile will look in all the same places as loadImage does.
  // this means you can find files that are in the data folder and the 
  // sketch folder. you can also pass an absolute path, or a URL.
  // Change the name of the audio file here and add it by clicking on "Sketch —> Import File"
  player = minim.loadFile("02.mp3"); 
}

void draw() {
   while (myPort.available() > 0) {
   myString = myPort.readStringUntil(lf);
    
    if (myString != null) {
      myString = myString.trim(); 
      if(myString.length() > 0) {
        println(myString); 
        if(myString.equals("D")){
          if(player.isPlaying() == false){
            player.play();
            } 
        }
         if(myString.equals("P")) {
              player.pause();
         }
      }
    }
  }
}