[java技术]输出音频和捕获音频

jackxiang 2006-10-5 13:19 | |

这是我原来用过的两段代码,输出音频和捕获音频。
构造器里的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)
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;
}
}


import java.io.*;
import javax.sound.sampled.*;
import java.net.*;

/**
* title: voicechat
* description: 音频捕捉(录音程序)
* copyright: copyright (c) 2001
* company:
* @author  你猜!
* @version 1.0
*/

class capture implements runnable {

targetdataline line;
thread thread;
socket s;
bufferedoutputstream captrueoutputstream;

capture(socket s){//构造器 取得socket以获得网络输出流
this.s=s;
}

public void start() {

thread = new thread(this);
thread.setname("capture");
thread.start();
}

public void stop() {
thread = null;
}

public void run() {

try {
captrueoutputstream=new bufferedoutputstream(s.getoutputstream());//建立输出流 此处可以加套压缩流用来压缩数据
}
catch (ioexception ex) {
return;
}

audioformat format =new audioformat(8000,16,2,true,true);//audioformat(float samplerate, int samplesizeinbits, int channels, boolean signed, boolean bigendian)
dataline.info info = new dataline.info(targetdataline.class,format);

try {
line = (targetdataline) audiosystem.getline(info);
line.open(format, line.getbuffersize());
} catch (exception ex) {
return;
}

byte[] data = new byte[1024];//此处的1024可以情况进行调整,应跟下面的1024应保持一致
int numbytesread=0;
line.start();

while (thread != null) {
numbytesread = line.read(data, 0,1024);//取数据(1024)的大小直接关系到传输的速度,一般越小越快,
try {
captrueoutputstream.write(data, 0, numbytesread);//写入网络流
}
catch (exception ex) {
break;
}
}

line.stop();
line.close();
line = null;

try {
captrueoutputstream.flush();
captrueoutputstream.close();
} catch (ioexception ex) {
ex.printstacktrace();
}
}
}

作者:jackxiang@向东博客 专注WEB应用 构架之美 --- 构架之美,在于尽态极妍 | 应用之美,在于药到病除
地址:https://jackxiang.com/post/218/
版权所有。转载时必须以链接形式注明作者和原始出处及本声明!

评论列表
发表评论

昵称

网址

电邮

打开HTML 打开UBB 打开表情 隐藏 记住我 [登入] [注册]