Stuck with Audio Player and controlP5
in
Contributed Library Questions
•
1 year ago
Hello all!
I was hoping someone could shed some light on what I may be doing wrong... I've struggled with this all night and have exhausted my searching options.
What I am trying to achieve is to open a file chooser when the user clicks the Open File button (which is a controlP5 button) and select an MP3 which then converts the path of the file chosen and stores that in a string which then can be played by pressing the Play button.
If I click Open File, it opens the file chooser but when I acknowledge the file, the sketch hangs.
Am I going about this the right way or is there a much simpler way?
Thank you kindly in advance!!!
I was hoping someone could shed some light on what I may be doing wrong... I've struggled with this all night and have exhausted my searching options.
What I am trying to achieve is to open a file chooser when the user clicks the Open File button (which is a controlP5 button) and select an MP3 which then converts the path of the file chosen and stores that in a string which then can be played by pressing the Play button.
If I click Open File, it opens the file chooser but when I acknowledge the file, the sketch hangs.
Am I going about this the right way or is there a much simpler way?
Thank you kindly in advance!!!
- import controlP5.*;
import ddf.minim.signals.*;
import ddf.minim.*;
import ddf.minim.analysis.*;
import ddf.minim.effects.*;
import ddf.minim.ugens.*;
import javax.swing.*;
ControlP5 cp5;
Minim minim;
AudioPlayer player;
File file;
String song = "";
void setup() {
size(800, 800);
cp5 = new ControlP5(this);
// create a new button with name 'Play'
cp5.addButton("Play", 1, 100, 400, 60, 19);
// and add Pause Button
cp5.addButton("Pause", 2, 100, 420, 60, 19);
// add Open button
cp5.addButton("Open", 3, 100, 440, 60, 10);
}
void draw()
{
background(0);
}
void controlEvent(ControlEvent theEvent) {
if (theEvent.isController()) {
if (theEvent.controller().name() == "Play") {
player.play();
}
if (theEvent.controller().name() == "Pause") {
player.pause();
}
if (theEvent.controller().name() == "Open") {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (Exception e) {
e.printStackTrace(); - }
JFileChooser fc = new JFileChooser();
int returnVal = fc.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
file = fc.getSelectedFile();
song = file.getPath();
minim = new Minim(this);
player = minim.loadFile(song, 512);
}
}
}
}
void stop()
{
// the AudioPlayer you got from Minim.loadFile()
player.close();
minim.stop();
// this calls the stop method that
// you are overriding by defining your own
// it must be called so that your application
// can do all the cleanup it would normally do
super.stop();
}
1