您好,欢迎访问三七文档
当前位置:首页 > 电子/通信 > 数据通信与网络 > java获取话筒音频并保存
ÏÂÃæʵÀý´úÂëAudioRecorder.javaÊÇ»ñÈ¡µçÄÔ»°Í²ÉùÒô²¢´¢´æΪÒôƵµÄ¹ý³ÌÏÂÃæ´úÂ븴Öƺó¿ÉÒÔÕýÈ·ÔËÐÐimportjavax.swing.*;importjava.awt.*;importjava.awt.event.*;importjava.io.*;importjavax.sound.sampled.*;publicclassAudioRecorderextendsJFrame{AudioFormataudioFormat;TargetDataLinetargetDataLine;finalJButtoncaptureBtn=newJButton(Capture);finalJButtonstopBtn=newJButton(Stop);finalJPanelbtnPanel=newJPanel();finalButtonGroupbtnGroup=newButtonGroup();finalJRadioButtonaifcBtn=newJRadioButton(AIFC);finalJRadioButtonaiffBtn=newJRadioButton(AIFF);finalJRadioButtonauBtn=newJRadioButton(AU,true);finalJRadioButtonsndBtn=newJRadioButton(SND);finalJRadioButtonwaveBtn=newJRadioButton(WAVE);publicstaticvoidmain(Stringargs[]){newAudioRecorder();}//endmainpublicAudioRecorder(){//constructorcaptureBtn.setEnabled(true);stopBtn.setEnabled(false);//RegisteranonymouslistenerscaptureBtn.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){captureBtn.setEnabled(false);stopBtn.setEnabled(true);//Captureinputdatafromthe//microphoneuntiltheStopbuttonis//clicked.captureAudio();}//endactionPerformed}//endActionListener);//endaddActionListener()stopBtn.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){captureBtn.setEnabled(true);stopBtn.setEnabled(false);//Terminatethecapturingofinputdata//fromthemicrophone.targetDataLine.stop();targetDataLine.close();}//endactionPerformed}//endActionListener);//endaddActionListener()//PutthebuttonsintheJFramegetContentPane().add(captureBtn);getContentPane().add(stopBtn);//IncludetheradiobuttonsinagroupbtnGroup.add(aifcBtn);btnGroup.add(aiffBtn);btnGroup.add(auBtn);btnGroup.add(sndBtn);btnGroup.add(waveBtn);//AddtheradiobuttonstotheJPanelbtnPanel.add(aifcBtn);btnPanel.add(aiffBtn);btnPanel.add(auBtn);btnPanel.add(sndBtn);btnPanel.add(waveBtn);//PuttheJPanelintheJFramegetContentPane().add(btnPanel);//FinishtheGUIandmakevisiblegetContentPane().setLayout(newFlowLayout());setTitle(Copyright2013,zhou);setDefaultCloseOperation(EXIT_ON_CLOSE);setSize(300,120);setVisible(true);}//endconstructor//Thismethodcapturesaudioinputfroma//microphoneandsavesitinanaudiofile.privatevoidcaptureAudio(){try{//GetthingssetupforcaptureaudioFormat=getAudioFormat();DataLine.InfodataLineInfo=newDataLine.Info(TargetDataLine.class,audioFormat);targetDataLine=(TargetDataLine)AudioSystem.getLine(dataLineInfo);//Createathreadtocapturethemicrophone//dataintoanaudiofileandstartthe//threadrunning.Itwillrununtilthe//Stopbuttonisclicked.Thismethod//willreturnafterstartingthethread.newCaptureThread().start();}catch(Exceptione){e.printStackTrace();System.exit(0);}//endcatch}//endcaptureAudiomethod//Thismethodcreatesandreturnsan//AudioFormatobjectforagivensetofformat//parameters.Iftheseparametersdon'twork//wellforyou,trysomeoftheother//allowableparametervalues,whichareshown//incommentsfollowingthedeclarations.privateAudioFormatgetAudioFormat(){floatsampleRate=8000.0F;//8000,11025,16000,22050,44100intsampleSizeInBits=16;//8,16intchannels=1;//1,2booleansigned=true;//true,falsebooleanbigEndian=false;//true,falsereturnnewAudioFormat(sampleRate,sampleSizeInBits,channels,signed,bigEndian);}//endgetAudioFormat//=============================================////Innerclasstocapturedatafrommicrophone//andwriteittoanoutputaudiofile.classCaptureThreadextendsThread{publicvoidrun(){AudioFileFormat.TypefileType=null;FileaudioFile=null;//Setthefiletypeandthefileextension//basedontheselectedradiobutton.if(aifcBtn.isSelected()){fileType=AudioFileFormat.Type.AIFC;audioFile=newFile(junk.aifc);}elseif(aiffBtn.isSelected()){fileType=AudioFileFormat.Type.AIFF;audioFile=newFile(junk.aif);}elseif(auBtn.isSelected()){fileType=AudioFileFormat.Type.AU;audioFile=newFile(junk.au);}elseif(sndBtn.isSelected()){fileType=AudioFileFormat.Type.SND;audioFile=newFile(junk.snd);}elseif(waveBtn.isSelected()){fileType=AudioFileFormat.Type.WAVE;audioFile=newFile(junk.wav);}//endiftry{targetDataLine.open(audioFormat);targetDataLine.start();AudioSystem.write(newAudioInputStream(targetDataLine),fileType,audioFile);}catch(Exceptione){e.printStackTrace();}//endcatch}//endrun}//endinnerclassCaptureThread//=============================================//}//endouterclassAudioRecorder.java
本文标题:java获取话筒音频并保存
链接地址:https://www.777doc.com/doc-1358146 .html