Javaawt錄音長(zhǎng)時(shí)間錄音(3).doc_第1頁(yè)
Javaawt錄音長(zhǎng)時(shí)間錄音(3).doc_第2頁(yè)
Javaawt錄音長(zhǎng)時(shí)間錄音(3).doc_第3頁(yè)
Javaawt錄音長(zhǎng)時(shí)間錄音(3).doc_第4頁(yè)
Javaawt錄音長(zhǎng)時(shí)間錄音(3).doc_第5頁(yè)
已閱讀5頁(yè),還剩11頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、Java awt錄音長(zhǎng)時(shí)間錄音( 3)import javax.sound.sampled.*;import javax.swing.JFrame;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import java.awt.Toolkit;import javax.swing.JPanel;import java.awt.event.ActionListener;import javax.sound.sampled.AudioInputStream; import javax.swing.JButt

2、on; import java.io.File;import java.util.Vector;import java.awt.BorderLayout;import javax.swing.border.EmptyBorder; import javax.swing.border.SoftBevelBorder; import javax.swing.BoxLayout; import java.awt.Dimension;import java.awt.event.ActionEvent;import javax.sound.sampled.AudioFileFormat; import

3、javax.sound.sampled.AudioFormat; import javax.sound.sampled.TargetDataLine;import javax.sound.sampled.AudioSystem; import java.io.IOException;import javax.sound.sampled.DataLine;import javax.sound.sampled.LineUnavailableException; import java.io.ByteArrayOutputStream; import java.io.ByteArrayInputSt

4、ream;/* see java錄音程序*/public class RecordTest extends JPanel implements ActionListener final int bufSize = 16384;Capture capture = new Capture();AudioInputStream audioInputStream;JButton captB;String fileName = untitled;String errStr;double duration, seconds;File file;Vector lines = new Vector();pub

5、lic RecordTest() setLayout(new BorderLayout();EmptyBorder eb = new EmptyBorder(5, 5, 5, 5);SoftBevelBorder sbb = newSoftBevelBorder(SoftBevelBorder.LOWERED); setBorder(new EmptyBorder(5, 5, 5, 5); JPanel p1 = new JPanel(); p1.setBorder(sbb);p1.setLayout(new BoxLayout(p1, BoxLayout.Y_AXIS);JPanel but

6、tonsPanel = new JPanel();buttonsPanel.setBorder(new EmptyBorder(10, 0, 5, 0);captB = addButton(Record, buttonsPanel, true);p1.add(buttonsPanel);add(p1);public void open() public void close() if (capture.thread != null)captB.doClick(0);private JButton addButton(String name, JPanel p,boolean state) JB

7、utton b = new JButton(name);b.addActionListener(this);b.setEnabled(state);p.add(b);return b;public void actionPerformed(ActionEvent e) Object obj = e.getSource();if (obj.equals(captB) if (captB.getText().startsWith(Record) file = null;capture.start();fileName = untitled;captB.setText(Stop); else lin

8、es.removeAllElements();capture.stop();captB.setText(Record);public void createAudioInputStream(File file, boolean updateComponents) if (file != null && file.isFile() try this.file = file;errStr = null;audioInputStream =AudioSystem.getAudioInputStream(file); fileName = file.getName();long mil

9、liseconds = (long) (audioInputStream.getFrameLength() * 1000) / audioInputStream.getFormat().getFrameRate();duration = milliseconds / 1000.0;if (updateComponents) catch (Exception ex) reportStatus(ex.toString(); else reportStatus(Audio file required.);public void saveToFile(String name,AudioFileForm

10、at.Type fileType) if (audioInputStream = null) reportStatus(No loaded audio to save); return; else if (file != null) createAudioInputStream(file, false);/ reset to the beginnning of the captured data try audioInputStream.reset(); catch (Exception e) reportStatus(Unable to reset stream + e); return;F

11、ile file = new File(fileName = name); try if (AudioSystem.write(audioInputStream, fileType, file)= -1) throw new IOException(Problems writing to file); catch (Exception ex) reportStatus(ex.toString();private void reportStatus(String msg) if (errStr = msg) != null) System.out.println(errStr);class Ca

12、pture implements Runnable TargetDataLine line;Thread thread;public void start() errStr = null;thread = new Thread(this);thread.setName(Capture);thread.start();public void stop() thread = null;private void shutDown(String message) if (errStr = message) != null && thread != null)thread = null;

13、captB.setText(Record);System.err.println(errStr);public void run() duration = 0;audioInputStream = null;/ get an AudioInputStream of the desired format for playbackAudioFormat.Encoding encoding = AudioFormat.Encoding.PCM_SIGNED;/ define the required attributes for our line,/ and make sure a compatib

14、le line is supported./ float rate = 44100f;/ int sampleSize = 16;/ String signedString = signed;/ boolean bigEndian = true;/ int channels = 2;float rate = 8000f;int sampleSize = 8;String signedString = signed;boolean bigEndian = true;int channels = 1;AudioFormat format = new AudioFormat(encoding, ra

15、te, sampleSize,channels, (sampleSize / 8) * channels, rate, bigEndian);DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);if (!AudioSystem.isLineSupported(info) shutDown(Line matching + info + not supported.); return;/ get an AudioInputStream of the desired format for playbacktry l

16、ine = (TargetDataLine) AudioSystem.getLine(info); line.open(format, line.getBufferSize(); catch (LineUnavailableException ex) shutDown(Unable to open the line: + ex); return; catch (SecurityException ex) shutDown(ex.toString(); return; catch (Exception ex) shutDown(ex.toString();return;/ play back t

17、he captured audio data ByteArrayOutputStream out = newByteArrayOutputStream();int frameSizeInBytes = format.getFrameSize();int bufferLengthInFrames = line.getBufferSize() / 8; int bufferLengthInBytes = bufferLengthInFrames *frameSizeInBytes;byte data = new bytebufferLengthInBytes;int numBytesRead;li

18、ne.start();while (thread != null) if (numBytesRead = line.read(data, 0, bufferLengthInBytes) = -1) break;out.write(data, 0, numBytesRead);/ we reached the end of the stream. stop and close theline.line.stop();line.close(); line = null;/ stop and close the output streamtry out.flush();out.close(); ca

19、tch (IOException ex) ex.printStackTrace();/ load bytes into the audio input stream for playback byte audioBytes = out.toByteArray(); ByteArrayInputStream bais = newByteArrayInputStream(audioBytes); audioInputStream = new AudioInputStream(bais,format,audioBytes.length / frameSizeInBytes); long millis

20、econds = (long)(audioInputStream.getFrameLength() * 1000) / format.getFrameRate();duration = milliseconds / 1000.0;try audioInputStream.reset(); catch (Exception ex) ex.printStackTrace();return;saveToFile(record.wav,AudioFileFormat.Type.WAVE);public static void main(String args) RecordTest test = ne

21、w RecordTest();test.open();JFrame f = new JFrame(Capture); f.addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e) System.exit(0););f.getContentPane().add(Center, test);f.pack();Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();int w = 500;int h = 340;f.setL

22、ocation(screenSize.width / 2 - w / 2,screenSize.height / 2 - h/ 2); f.setSize(w, h); f.show();播放 package pany.test3;import java.io.*;import javax.sound.sampled.AudioSystem; import javax.sound.sampled.AudioInputStream; import javax.sound.sampled.AudioFormat; import javax.sound.sampled.SourceDataLine;

23、 import javax.sound.sampled.DataLine; import javax.sound.sampled.FloatControl; import javax.sound.sampled.TargetDataLine;public class RecordAndPlay static volatile boolean stop=false;public static void main(String args) Play();/播放音頻文件public static void Play() try AudioFormat audioFormat =/ new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 44100F,/8, 1, 1, 44100F, false);newAudioFormat(AudioFormat.Encoding.PCM_SIGNED,44100F, 16, 2, 4, 44100F, true); DataLine.Info info = newDataLine.Info(TargetDataLine.class,audioForm

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論