📄 voicesend.java
字号:
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineEvent;
import javax.sound.sampled.LineListener;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.TargetDataLine;
public class VoiceSend implements Runnable,LineListener
{
final static int BUFSIZE=512;
byte dataBuf[]=new byte[BUFSIZE];
String ip;
InetAddress RemoteHost=null;
DatagramSocket UDPClientSocket;
DatagramPacket ClientPacket=null;
AudioFormat format = null;
Thread t = null;
TargetDataLine tdLine = null;
SourceDataLine sdLine = null;
DataLine.Info inInfo = null;
DataLine.Info outInfo=null;
VoiceSend() throws LineUnavailableException, SecurityException
{
format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 8000F, 8, 1, 1, 8000F, true);
inInfo = new DataLine.Info(TargetDataLine.class, format);
t = null;
outInfo = new DataLine.Info(SourceDataLine.class,format);
try
{
tdLine = (TargetDataLine)AudioSystem.getLine(inInfo);
tdLine.open(format, tdLine.getBufferSize());
tdLine.addLineListener(this);
}
catch (Exception e)
{
}
}
public void setIP(String ip)
{
this.ip=ip;
}
public void send(byte abyte0[],int inBytes)
{
try
{
dataBuf=abyte0;
try
{
UDPClientSocket=new DatagramSocket(3000);
} catch (SocketException e) {
e.printStackTrace();
}
try
{
ClientPacket=new DatagramPacket(dataBuf,inBytes,InetAddress.getByName(this.ip),3333);
}
catch (UnknownHostException e)
{
e.printStackTrace();
}
UDPClientSocket.send(ClientPacket);
for(int i=0;i<dataBuf.length;i++)
dataBuf[i]=0;
UDPClientSocket.close();
UDPClientSocket=null;
}
catch (NullPointerException e)
{
}
catch (IOException e)
{
e.printStackTrace();
}
}
public void start()
{
t = new Thread(this);
t.start();
}
public void stop()
{
t = null;
}
public void shutdown()
{
if(tdLine != null)
{
if(tdLine.isActive())
tdLine.stop();
tdLine.close();
tdLine = null;
}
}
public void run()
{
Thread thisThread = Thread.currentThread();
byte abyte0[] = new byte[BUFSIZE];
tdLine.start();
while(t == thisThread)
{
try
{
int i;
if((i = tdLine.read(abyte0, 0, BUFSIZE)) == -1)
break;
this.send(abyte0,i);
}
catch(ArrayIndexOutOfBoundsException e)
{}
catch(NullPointerException er)
{}
}
}
@Override
public void update(LineEvent event)
{
// TODO Auto-generated method stub
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -