使用来自 processing.org 的示例文件(在 Win8 上),我得到了“从麦克风录制”和 play() 声音工作正常。我只需要一次录制 10 到 30 秒。但是现在找不到任何方法来关闭现有的录音并录制新的录音。
我已经尝试了各种方法。我希望能够按另一个键,再次按“r”并再记录几秒钟,例如语言词汇练习等。
我正在使用来自隔间.net/minim 的最小示例代码。官方文档只是列出了beginRecord/endRecord,但是没有办法关闭现有的录制并开始另一个。没有诸如 recorder.close() 或 .reset/restart 之类的东西。
import ddf.minim.*;
import ddf.minim.ugens.*;
Minim minim;
// for recording
AudioInput in;
AudioRecorder recorder;
boolean recorded;
// for playing back
AudioOutput out;
FilePlayer player;
void setup()
{ size(512, 200, P3D);
minim = new Minim(this);
in = minim.getLineIn(Minim.STEREO, 2048);
// create an AudioRecorder
recorder = minim.createRecorder(in, "myrecording.wav");
// get an output
out = minim.getLineOut( Minim.STEREO );
textFont(createFont("Arial", 24));
}
void draw()
{ background(255,240,128);
stroke(32);
if ( recorder.isRecording() )
{ text("Now recording, press the r key to stop recording.", 5, 15); }
else if ( !recorded )
{ text("Press the R key to start recording.", 5, 15); }
else
{ text("Press the S key to save the recording to disk and play it back in the sketch.", 5, 15); }
}
//end draw
void keyReleased()
{
if ( !recorded && key == 'r' )
{ // to indicate that you want to start or stop capturing audio data,
if ( recorder.isRecording() )
{ recorder.endRecord();
recorded = true; }
else
{ recorder.beginRecord(); }
}
if ( recorded && key == 's' )
{ // now write it to file
// case of buffered recording, will freeze sketch for a bit if buffer is large
// case of streamed recording, will not freeze all that is being done
// all that is being done is closing the file.
// save returns the recorded audio in an AudioRecordingStream,
// which we can then play with a FilePlayer
if ( player != null )
{ player.unpatch( out );
player.close(); }
player = new FilePlayer( recorder.save() );
player.patch( out );
player.play();
}
// my addition — this works for play-again
if ( recorded && key == 'p' )
{ player.rewind();
player.play();
}
我尝试关闭 minim 并重新启动它,但随后它抱怨“未使用局部变量 xxx”,如下所示:不起作用。
if ( key == 'x' )
{ minim.stop();
minim = new Minim(this);
AudioInput in;
AudioRecorder recorder;
AudioOutput out;
FilePlayer player; }