Minim Audioplayer object player only plays once - why?
in
Contributed Library Questions
•
1 year ago
Hi there,
I was hoping someone could give me a heads up on why the minim Audioplayer object player1 only plays once in the serialEvent method of my code? Although the incoming inString.equals("o") is received the file box4a.wav is only played the first time the string "o" is received. I've checked that it enters the if statement, but the player doesn't swing...
Any advice and suggestions will be greatly appreciated :) Thanks!
I was hoping someone could give me a heads up on why the minim Audioplayer object player1 only plays once in the serialEvent method of my code? Although the incoming inString.equals("o") is received the file box4a.wav is only played the first time the string "o" is received. I've checked that it enters the if statement, but the player doesn't swing...
Any advice and suggestions will be greatly appreciated :) Thanks!
- /**
* This sketch demonstrates how to an <code>AudioRecorder</code> to record audio to disk.
* To use this sketch you need to have something plugged into the line-in on your computer. Press 'r' to toggle
* recording on and off and the press 's' to save to disk. The recorded file will be placed in the sketch folder of
* the sketch.
*/
import processing.serial.*;
import ddf.minim.*;
import JMyron.*;
Serial myPort; //serial communication port
Minim minimrec;
Minim minimplay;
AudioInput in;
AudioRecorder recorder;
AudioPlayer player1;
//AudioPlayer player2;
//AudioPlayer player3;
JMyron m;//a camera object
String timestamp = "never";
String url = "http://moxi.dk/test.php";
boolean lifted = false;
boolean flag = true;
int test = 0;
void setup()
{
//setup communication to arduino. The first port is [0]
// Print a list of the serial ports, for debugging purposes:
println(Serial.list());
String portName = Serial.list()[1];
myPort = new Serial(this, portName, 9600);
// setup cam
size(320,240); //size of image frame
m = new JMyron();//make a new instance of the object
m.start(width,height);//start a capture at 320x240
m.findGlobs(0);//disable the intelligence to speed up frame rate
println("Myron " + m.version());
// setup sound recording
minimrec = new Minim(this);
// get a stereo line-in: sample buffer length of 2048
// default sample rate is 44100, default bit depth is 16
in = minimrec.getLineIn(Minim.STEREO, 2048);
// create a recorder that will record from the input to the filename specified, using buffered recording
// buffered recording means that all captured audio will be written into a sample buffer
// then when save() is called, the contents of the buffer will actually be written to a file
// the file will be located in the sketch's root folder.
recorder = minimrec.createRecorder(in, timestamp+".wav", true);
minimplay = new Minim(this);
// load a file, give the AudioPlayer buffers that are 2048 samples long
player1 = minimplay.loadFile("box4a.wav", 2048);
// player2 = minimplay.loadFile("box4b.wav", 2048);
// player3 = minimplay.loadFile("box4c.wav", 2048);
}
void draw()
{
m.update();//update the camera view
int[] img = m.image(); //get the normal image of the camera
loadPixels();
for(int i=0;i<width*height;i++){ //loop through all the pixels
pixels[i] = img[i]; //draw each pixel to the screen
}
updatePixels();
}
void serialEvent(Serial myPort)
{
String inString = myPort.readStringUntil(' ');
if (inString!=null)
{
inString = trim(inString);
println(inString);
if (inString.equals("o"))
{
player1.play(); //play intro wav
println("lifted");
lifted = true;
}
if (lifted && inString.equals("T"))
{
println("accepted");
println("finished");
}
else if (lifted && inString.equals("F"))
{
println("incorrect pin");
//player3.play(); //play wrong code wav
}
}
}
void keyReleased()
{
if (key == '*')
{
String tmp = timestamp+".jpg";
saveFrame(tmp);
println("saving img to web");
// open a file and read its binary data
byte data[] = loadBytes(timestamp+".jpg");
saveToWeb_saveFile(timestamp(), "jpg", data);
}
if ( key == '#' )
{
//timeStamp = "now";
// to indicate that you want to start or stop capturing audio data, you must call
// beginRecord() and endRecord() on the AudioRecorder object. You can start and stop
// as many times as you like, the audio data will be appended to the end of the buffer
// (in the case of buffered recording) or to the end of the file (in the case of streamed recording).
if ( recorder.isRecording() )
{
recorder.endRecord();
println("done recording");
}
else
{
recorder = minimrec.createRecorder(in, timestamp+".wav", true);
recorder.beginRecord();
println("begin recording");
}
}
if ( key == 's' )
{
// we've filled the file out buffer,
// now write it to the file we specified in createRecorder
// in the case of buffered recording, if the buffer is large,
// this will appear to freeze the sketch for sometime
// in the case of streamed recording,
// it will not freeze as the data is already in the file and all that is being done
// is closing the file.
// the method returns the recorded audio as an AudioRecording,
// see the example AudioRecorder >> RecordAndPlayback for more about that
recorder.save();
// open a file and read its binary data
byte data[] = loadBytes(timestamp+".wav");
saveToWeb_saveFile(timestamp(), "wav", data);
println("Done saving.");
}
}
void stop()
{
// always close Minim audio classes when you are done with them
in.close();
if ( player1 != null )
{
player1.close();
}/*
if ( player2 != null )
{
player2.close();
}
if ( player3 != null )
{
player3.close();
}*/
minimrec.stop();
minimplay.stop();
m.stop();//stop the object
super.stop();
}
void saveToWeb_saveFile(String title, String ext, byte[] data)
{
println("SAVING File START");
postData(title,ext,data);
println("SAVING File STOP");
}
String timestamp()
{
Calendar now = Calendar.getInstance();
return String.format("%1$ty%1$tm%1$td_%1$tH%1$tM%1$tS", now);
}
void postData(String title, String ext, byte[] bytes)
{
try{
URL u = new URL(url+"?title="+title+"&ext="+ext);
URLConnection c = u.openConnection();
// post multipart data
c.setDoOutput(true);
c.setDoInput(true);
c.setUseCaches(false);
// set request headers
c.setRequestProperty("Content-Type", "multipart/form-data; boundary=AXi93A");
// open a stream which can write to the url
DataOutputStream dstream = new DataOutputStream(c.getOutputStream());
// write content to the server, begin with the tag that says a content element is comming
dstream.writeBytes("--AXi93A ");
// discribe the content
dstream.writeBytes("Content-Disposition: form-data; name="data"; filename="whatever" Content-Type: image/jpeg Content-Transfer-Encoding: binary ");
dstream.write(bytes,0,bytes.length);
// close the multipart form request
dstream.writeBytes(" --AXi93A-- ");
dstream.flush();
dstream.close();
// read the output from the URL
try{
BufferedReader in = new BufferedReader(new InputStreamReader(c.getInputStream()));
String sIn = in.readLine();
boolean b = true;
while(sIn!=null){
if(sIn!=null){
System.out.println(sIn);
}
sIn = in.readLine();
}
}
catch(Exception e){
e.printStackTrace();
}
}
catch(Exception e){
e.printStackTrace();
}
}
2