Java awt 录音 长时间 录音 3Word文档下载推荐.docx
《Java awt 录音 长时间 录音 3Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《Java awt 录音 长时间 录音 3Word文档下载推荐.docx(10页珍藏版)》请在冰豆网上搜索。
importjava.io.ByteArrayOutputStream;
importjava.io.ByteArrayInputStream;
/**
*@seejava录音程序
*
*/
publicclassRecordTestextendsJPanelimplementsActionListener{
finalintbufSize=16384;
Capturecapture=newCapture();
AudioInputStreamaudioInputStream;
JButtoncaptB;
StringfileName="
untitled"
;
StringerrStr;
doubleduration,seconds;
Filefile;
Vectorlines=newVector();
publicRecordTest(){
setLayout(newBorderLayout());
EmptyBordereb=newEmptyBorder(5,5,5,5);
SoftBevelBordersbb=newSoftBevelBorder(SoftBevelBorder.LOWERED);
setBorder(newEmptyBorder(5,5,5,5));
JPanelp1=newJPanel();
p1.setBorder(sbb);
p1.setLayout(newBoxLayout(p1,BoxLayout.Y_AXIS));
JPanelbuttonsPanel=newJPanel();
buttonsPanel.setBorder(newEmptyBorder(10,0,5,0));
captB=addButton("
Record"
buttonsPanel,true);
p1.add(buttonsPanel);
add(p1);
}
publicvoidopen(){
publicvoidclose(){
if(capture.thread!
=null)
captB.doClick(0);
privateJButtonaddButton(Stringname,JPanelp,booleanstate){
JButtonb=newJButton(name);
b.addActionListener(this);
b.setEnabled(state);
p.add(b);
returnb;
publicvoidactionPerformed(ActionEvente){
Objectobj=e.getSource();
if(obj.equals(captB)){
if(captB.getText().startsWith("
)){
file=null;
capture.start();
fileName="
captB.setText("
Stop"
);
}else{
lines.removeAllElements();
capture.stop();
publicvoidcreateAudioInputStream(Filefile,booleanupdateComponents){
if(file!
=null&
amp;
&
file.isFile()){
try{
this.file=file;
errStr=null;
audioInputStream=AudioSystem.getAudioInputStream(file);
fileName=file.getName();
longmilliseconds=(long)((audioInputStream.getFrameLength()*1000)/audioInputStream
.getFormat().getFrameRate());
duration=milliseconds/1000.0;
if(updateComponents){
}catch(Exceptionex){
reportStatus(ex.toString());
reportStatus("
Audiofilerequired."
publicvoidsaveToFile(Stringname,AudioFileFormat.TypefileType){
if(audioInputStream==null){
Noloadedaudiotosave"
return;
}elseif(file!
=null){
createAudioInputStream(file,false);
//resettothebeginnningofthecaptureddata
audioInputStream.reset();
}catch(Exceptione){
Unabletoresetstream"
+e);
Filefile=newFile(fileName=name);
if(AudioSystem.write(audioInputStream,fileType,file)==-1){
thrownewIOException("
Problemswritingtofile"
privatevoidreportStatus(Stringmsg){
if((errStr=msg)!
System.out.println(errStr);
classCaptureimplementsRunnable{
TargetDataLineline;
Threadthread;
publicvoidstart(){
thread=newThread(this);
thread.setName("
Capture"
thread.start();
publicvoidstop(){
thread=null;
privatevoidshutDown(Stringmessage){
if((errStr=message)!
thread!
System.err.println(errStr);
publicvoidrun(){
duration=0;
audioInputStream=null;
//getanAudioInputStreamofthedesiredformatforplayback
AudioFormat.Encodingencoding=AudioFormat.Encoding.PCM_SIGNED;
//definetherequiredattributesforourline,
//andmakesureacompatiblelineissupported.
//floatrate=44100f;
//intsampleSize=16;
//StringsignedString="
signed"
//booleanbigEndian=true;
//intchannels=2;
floatrate=8000f;
intsampleSize=8;
StringsignedString="
booleanbigEndian=true;
intchannels=1;
AudioFormatformat=newAudioFormat(encoding,rate,sampleSize,
channels,(sampleSize/8)*channels,rate,bigEndian);
DataLine.Infoinfo=newDataLine.Info(TargetDataLine.class,format);
if(!
AudioSystem.isLineSupported(info)){
shutDown("
Linematching"
+info+"
notsupported."
line=(TargetDataLine)AudioSystem.getLine(info);
line.open(format,line.getBufferSize());
}catch(LineUnavailableExceptionex){
Unabletoopentheline:
"
+ex);
}catch(SecurityExceptionex){
shutDown(ex.toString());
//playbackthecapturedaudiodata
ByteArrayOutputStreamout=newByteArrayOutputStream();
intframeSizeInBytes=format.getFrameSize();
intbufferLengthInFrames=line.getBufferSize()/8;
intbufferLengthInBytes=bufferLengthInFrames*frameSizeInBytes;
byte[]data=newbyte[bufferLengthInBytes];
intnumBytesRead;
line.start();
while(thread!
if((numBytesRead=line.read(data,0,bufferLengthInBytes))==-1){
break;
out.write(data,0,numBytesRead);
//wereachedtheendofthestream.stopandclosetheline.
line.stop();
line.close();
line=null;
//stopandclosetheoutputstream
out.flush();
out.close();
}catch(IOExceptionex){
ex.printStackTrace();
//loadbytesintotheaudioinputstreamforplayback
byteaudioBytes[]=out.toByteArray();
ByteArrayInputStreambais=newByteArrayInputStream(audioBytes);
audioInputStream=newAudioInputStream(bais,format,
audioBytes.length/frameSizeInBytes);
longmilliseconds=(long)((audioInputStream.getFrameLength()*1000)/format
.getFrameRate());
saveToFile("
record.wav"
AudioFileFormat.Type.WAVE);
publicstaticvoidmain(String[]args){
RecordTesttest=newRecordTest();
test.open();
JFramef=newJFrame("
f.addWindowListener(newWindowAdapter(){
publicvoidwindowClosing(WindowEvente){
System.exit(0);
});
f.getContentPane().add("
Center"
test);
f.pack();
DimensionscreenSize=Toolkit.getDefaultToolkit().getScreenSize();
intw=500;
inth=340;
f.setLocation(screenSize.width/2-w/2,screenSize.height/2-h
/2);
f.setSize(w,h);
f.show();
}
播放packagepany.test3;
importjava.io.*;
importjavax.sound.sampled.SourceDataLine;
importjavax.sound.sampled.FloatControl;
publicclassRecordAndPlay{
staticvolatilebooleanstop=false;
publicstaticvoidmain(String[]args){
Play();
}
//播放音频文件
publicstaticvoidPlay(){
try{
AudioFormataudioFormat=
//newAudioFormat(AudioFormat.Encoding.PCM_SIGNED,44100F,
//8,1,1,44100F,false);
newAudioFormat(AudioFormat.Encoding.PCM_SIGNED,44100F,16,2,4,44100F,true);
DataLine.Infoinfo=newDataLine.Info(TargetDataLine.class,audioFormat);
TargetDataLinetargetDataLine=(TargetDataLine)AudioSystem.getLine(info);
targetDataLine.open(audioFormat);
finalSourceDataLinesourceDataLine;
info=newDataLine.Info(SourceDataLine.class,audioFormat);
sourceDataLine=(SourceDataLine)AudioSystem.getLine(info);
sourceDataLine.open(audioFormat);
targetDataLine.start();
sourceDataLine.start();
FloatControlfc=(FloatControl)sourceDataLine.getControl(FloatControl.Type.MASTER_GAIN);
doublevalue=2;
floatdB=(float)
(Math.log(value==0.0?
0.0001:
value)/Math.log(10.0)*20.0);
fc.setValue(dB);
intnByte=0;
finalintbufSize=4*100;
byte[]buffer=newbyte[bufSize];
while(nByte!
=-1){
//System.in.read();
nByte=targetDataLine.read(buffer,0,bufSize);
sourceDataLine.write(buffer,0,nByte);
}sourceDataLine.stop();
}catch(Exceptione){
e.printStackTrace();