這是我原來用過的兩段代碼,輸出音頻和捕獲音頻。
構造器裡的socket是用來接受來自網絡的音頻數據。不做網絡音頻可以去掉它。
希望能與大家分享經驗。8-)
import java.io.*;
import javax.sound.sampled.*;
import java.net.*;
/**
* Title: VoiceChat
* Description: 輸出音頻(放音程序)
* Copyright: Copyright (c) 2001
* Company:
* @author 你猜!
* @version 1.0
*/
class Playback implements Runnable {
final int bufSize = 16384;
SourceDataLine line;
Thread thread;
Socket s;
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)