?? playback.java
字號:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package voice;/** * * @author Administrator *///下面是音頻輸出的代碼://Playback.javaimport java.io.*;import javax.sound.sampled.*;import java.net.*;public class Playback implements Runnable { final int bufSize = 16384; SourceDataLine line; Thread thread; Socket s; public Playback(Socket s){//構造器 取得socket以獲得網絡輸入流 this.s=s; } public void start() { thread = new Thread(this); thread.setName("Playback"); thread.start(); } public void stop() { thread = null; } public void run() { AudioFormat format =new AudioFormat(8000,16,2,true,true);//AudioFormat(float sampleRate, int sampleSizeInBits, int channels, boolean signed, boolean bigEndian) BufferedInputStream playbackInputStream; try { playbackInputStream=new BufferedInputStream(new AudioInputStream(s.getInputStream(),format,2147483647));//封裝成音頻輸出流,如果網絡流是經過壓縮的需在此加套解壓流 } catch (IOException ex) { return; } DataLine.Info info = new DataLine.Info(SourceDataLine.class,format); try { line = (SourceDataLine) AudioSystem.getLine(info); line.open(format, bufSize); } catch (LineUnavailableException ex) { return; } byte[] data = new byte[1024];//此處數組的大小跟實時性關系不大,可根據情況進行調整 int numBytesRead = 0; line.start(); while (thread != null) { try{ numBytesRead = playbackInputStream.read(data); line.write(data, 0,numBytesRead); } catch (IOException e) { break; } } if (thread != null) { line.drain(); } line.stop(); line.close(); line = null; }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -