文档介绍:作者:wflm
email: wflm@
日期:6/22/2001 2:04:31 AM
这是我原来用过的两段代码,输出音频和捕获音频。
构造器里的socket是用来接受来自网络的音频数据。不做网络音频可以去掉它。
希望能与大家分享经验。8-)
import .*;
import .*;
import .*;
/**
Title: VoiceChat
Description: 输出音频(放音程序)
Copyright: Copyright (c) 2001
Company:
***@author 你猜!
***@version
*/
class Playback implements Runnable {
final int bufSize = 16384;
SourceDataLine line;
Thread thread;
Socket s;
Playback(Socket s){//构造器取得socket以获得网络输入流
=s;
}
public void start() {
thread = new Thread(this);
("Playback");
();
}
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((),format,2147483647));//封装成音频输出流,如果网络流是经过压缩的需在此加套解压流
}
catch (IOException ex) {
return;
}
info = new (,format);
try {
line = (SourceDataLine) (info);
(format, bufSize);
} catch (LineUnavailableException ex) {
return;
}
byte[] data = new byte[1024];//此处数组的大小跟实时性关系不大,可根据情况进行调整
int numBytesRead = 0;
();
while (thread != null) {
try{
numBytesRead = playbackInputSt