问题描述
在哪里有开源的项目```想下的看看``````
解决方案
解决方案二:
java语音聊天程序集成了java文本聊天和java语音聊天。///服务器界面importjava.io.*;importjava.awt.*;importjava.awt.event.*;importjava.net.*;publicclassServerextendsFrame{TextFieldtf=newTextField(20);TextAreata=newTextArea();Buttonsend=newButton("send");ButtonvoiceChat=newButton("voiceChat");ServerSocketserSocket;Socketclient;InputStreamin;BufferedReaderbr;OutputStreamout;BufferedWriterbw;publicServer(){super("Server");add("North",tf);add("Center",ta);add("South",send);add("East",voiceChat);setBackground(Color.yellow);setSize(250,250);show();addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente){dispose();System.exit(0);}});try{ServerSocketserSocket=newServerSocket(5000);client=serSocket.accept();ta.append("clienthost"+client.getInetAddress().getHostName()+"nn");in=client.getInputStream();br=newBufferedReader(newInputStreamReader(in));out=client.getOutputStream();bw=newBufferedWriter(newOutputStreamWriter(out));}catch(Exceptione){}while(true){try{byte[]buf=newbyte[200];in.read(buf);Stringstr=newString(buf);ta.append("Clientsay:"+str);ta.append("n");}catch(IOExceptione){}}}publicbooleanaction(Eventevt,Objectarg){if(evt.target.equals(send)){try{Stringstr=tf.getText();byte[]buf=str.getBytes();tf.setText(null);out.write(buf);ta.append("Isay:"+str);ta.append("n");}catch(IOExceptionioe){}}elseif(evt.target.equals(voiceChat)){try{ServerSocketserSock=newServerSocket(6000);Socketcli=serSock.accept();Playbackplayer=newPlayback(cli);player.start();}catch(Exceptione){}}returntrue;}publicstaticvoidmain(String[]args){Serverserver=newServer();}}//客户端界面importjava.io.*;importjava.awt.*;importjava.awt.event.*;importjava.net.*;publicclassClientextendsFrame{TextFieldtf=newTextField(20);TextAreata=newTextArea();Buttonsend=newButton("send");ButtonvoiceChat=newButton("voiceChat");Socketclient;InputStreamin;OutputStreamout;BufferedReaderbr;BufferedWriterbw;publicClient(){super("Client");add("North",tf);add("Center",ta);add("South",send);add("East",voiceChat);setSize(250,250);show();addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente){dispose();System.exit(0);}});try{Socketclient=newSocket("127.0.0.1",5000);ta.append("Connectto:"+client.getInetAddress().getHostName()+"nn");in=client.getInputStream();br=newBufferedReader(newInputStreamReader(in));out=client.getOutputStream();bw=newBufferedWriter(newOutputStreamWriter(out));}catch(IOExceptionioe){}while(true){try{byte[]buf=newbyte[200];in.read(buf);Stringstr=newString(buf);ta.append("Serversay:"+str);ta.append("n");}catch(IOExceptione){System.out.print(e.getMessage());}}}publicbooleanaction(Eventevt,Objectarg){if(evt.target.equals(send)){try{Stringstr=tf.getText();byte[]buf=str.getBytes();tf.setText(null);out.write(buf);ta.append("Isay:"+str);ta.append("n");}catch(IOExceptionioe){System.out.print(ioe.getMessage());}}elseif(evt.target.equals(voiceChat)){try{Socketcli=newSocket("127.0.0.1",6000);Capturecap=newCapture(cli);cap.start();}catch(Exceptione){}}returntrue;}publicstaticvoidmain(String[]args){Clientclient=newClient();}}//音频捕获部分,//Capture.javaimportjava.io.*;importjavax.sound.sampled.*;importjava.net.*;/***Title:VoiceChat*Description:音频捕捉(录音程序)*Copyright:Copyright(c)2001*Company:*@author*@version1.0*/classCaptureimplementsRunnable{TargetDataLineline;Threadthread;Sockets;BufferedOutputStreamcaptrueOutputStream;Capture(Sockets){//构造器取得socket以获得网络输出流this.s=s;}publicvoidstart(){thread=newThread(this);thread.setName("Capture");thread.start();}publicvoidstop(){thread=null;}publicvoidrun(){try{captrueOutputStream=newBufferedOutputStream(s.getOutputStream());//建立输出流此处可以加套压缩流用来压缩数据}catch(IOExceptionex){return;}AudioFormatformat=newAudioFormat(8000,16,2,true,true);//AudioFormat(floatsampleRate,intsampleSizeInBits,intchannels,booleansigned,booleanbigEndian)DataLine.Infoinfo=newDataLine.Info(TargetDataLine.class,format);try{line=(TargetDataLine)AudioSystem.getLine(info);line.open(format,line.getBufferSize());}catch(Exceptionex){return;}byte[]data=newbyte[1024];//此处的1024可以情况进行调整,应跟下面的1024应保持一致intnumBytesRead=0;line.start();while(thread!=null){numBytesRead=line.read(data,0,128);//取数据(1024)的大小直接关系到传输的速度,一般越小越快,try{captrueOutputStream.write(data,0,numBytesRead);//写入网络流}catch(Exceptionex){break;}}line.stop();line.close();line=null;try{captrueOutputStream.flush();captrueOutputStream.close();}catch(IOExceptionex){ex.printStackTrace();}}}//下面是音频输出的代码://Playback.javaimportjava.io.*;importjavax.sound.sampled.*;importjava.net.*;/***Title:VoiceChat*Description:输出音频(放音程序)*Copyright:Copyright(c)2001*Company:*@author*@version1.0*/classPlaybackimplementsRunnable{finalintbufSize=16384;SourceDataLineline;Threadthread;Sockets;Playback(Sockets){//构造器取得socket以获得网络输入流this.s=s;}publicvoidstart(){thread=newThread(this);thread.setName("Playback");thread.start();}publicvoidstop(){thread=null;}publicvoidrun(){AudioFormatformat=newAudioFormat(8000,16,2,true,true);//AudioFormat(floatsampleRate,intsampleSizeInBits,intchannels,booleansigned,booleanbigEndian)BufferedInputStreamplaybackInputStream;try{playbackInputStream=newBufferedInputStream(newAudioInputStream(s.getInputStream(),format,2147483647));//封装成音频输出流,如果网络流是经过压缩的需在此加套解压流}catch(IOExceptionex){return;}DataLine.Infoinfo=newDataLine.Info(SourceDataLine.class,format);try{line=(SourceDataLine)AudioSystem.getLine(info);line.open(format,bufSize);}catch(LineUnavailableExceptionex){return;}byte[]data=newbyte[1024];//此处数组的大小跟实时性关系不大,可根据情况进行调整intnumBytesRead=0;line.start();while(thread!=null){try{numBytesRead=playbackInputStream.read(data);line.write(data,0,numBytesRead);}catch(IOExceptione){break;}}if(thread!=null){line.drain();}line.stop();line.close();line=null;}}
解决方案三:
该回复于2010-12-09 16:07:09被版主删除
解决方案四:
谢谢``能在浏览器上适用吗
解决方案五:
该回复于2010-12-10 09:43:47被版主删除
解决方案六:
这样可以吗?
解决方案七:
该回复于2010-12-10 09:42:33被版主删除