标签:
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package n.audio.chart; import java.io.IOException; import java.net.DatagramPacket; import java.net.InetAddress; import java.net.MulticastSocket; /** * @author chch87 发送UDP报文的基类,扩展出MessageSender和SoundSender */ public class UDPSender { protected MulticastSocket s; protected InetAddress group; protected int port; protected DatagramPacket dgp; public UDPSender(String groupAddress, int port) { this.port = port; try { s = new MulticastSocket(); group = InetAddress.getByName(groupAddress);//group一定要是个组播组!如231.0.0.1 } catch (IOException e) { e.printStackTrace(); } dgp = new DatagramPacket(new byte[0], 0, group, port); } public void close() { if (s != null) { s.close(); } } protected void send(byte[] buffer) { dgp.setData(buffer); dgp.setLength(buffer.length); try { s.send(dgp); } catch (IOException e) { e.printStackTrace(); } } }
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package n.audio.chart; import java.io.IOException; import java.net.DatagramPacket; import java.net.InetAddress; import java.net.MulticastSocket; /** * @author chch87 接受UDP报文的基类,扩展出MessageReceiver和SoundReceiver */ public class UDPReceiver { protected MulticastSocket s; protected InetAddress group; protected byte[] buffer; protected DatagramPacket dgp; protected int port; protected int bufferSize; protected boolean isClose; public UDPReceiver(String groupAddress, int port, int bufferSize) { try { s = new MulticastSocket(port); group = InetAddress.getByName(groupAddress);//group一定要是个组播组!如231.0.0.1 s.joinGroup(group); //s.setLoopbackMode(true);//在局域网测试时,可以将禁止回调打开 } catch (IOException e) { e.printStackTrace(); } buffer = new byte[bufferSize]; dgp = new DatagramPacket(buffer, bufferSize); isClose = false; this.port = port; this.bufferSize = bufferSize; } public void close() { if (s != null && group != null) { try { s.leaveGroup(group); s.close(); } catch (IOException e) { e.printStackTrace(); } isClose = true; } } public void contect() { if (isClose) { try { s = new MulticastSocket(port); s.joinGroup(group); } catch (IOException e) { e.printStackTrace(); } buffer = new byte[bufferSize]; dgp = new DatagramPacket(buffer, bufferSize); isClose = false; } } protected byte[] receive() { try { s.receive(dgp); } catch (IOException e) { e.printStackTrace(); } return buffer; } protected int getDgpLength() { return dgp.getLength(); } protected String getIP() { return dgp.getAddress().toString(); } }
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package n.audio.chart; import javax.sound.sampled.AudioFormat; import javax.sound.sampled.AudioSystem; import javax.sound.sampled.DataLine; import javax.sound.sampled.LineUnavailableException; import javax.sound.sampled.TargetDataLine; /** * @author Hello 放音部分 */ public class SoundSender extends UDPSender implements Runnable { private TargetDataLine line; private final int bufferLength; private Thread thread; private boolean isStart; public SoundSender(String groupAddress, int port, int bufferLength) { super(groupAddress, port); AudioFormat format = new AudioFormat(8000, 16, 2, true, true); DataLine.Info info = new DataLine.Info(TargetDataLine.class, format); try { line = (TargetDataLine) AudioSystem.getLine(info); line.open(format, line.getBufferSize()); } catch (LineUnavailableException e) { e.printStackTrace(); } this.bufferLength = bufferLength; isStart = false; } @Override public void run() { byte[] buffer = new byte[bufferLength]; while (isStart && !thread.isInterrupted()) { line.read(buffer, 0, buffer.length);//接受麦的数据写入buffer send(buffer); } } public void start() { if (thread == null || !thread.isAlive()) { thread = new Thread(this); line.start(); thread.start(); isStart = true; } } public void stop() { thread.interrupt(); line.stop(); isStart = false; } }
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package n.audio.chart; import javax.sound.sampled.AudioFormat; import javax.sound.sampled.AudioSystem; import javax.sound.sampled.DataLine; import javax.sound.sampled.LineUnavailableException; import javax.sound.sampled.SourceDataLine; public class SoundReceiver extends UDPReceiver implements Runnable { private SourceDataLine line; private Thread thread; private boolean isStart; public SoundReceiver(String groupAddress, int port, int bufferSize) { super(groupAddress, port, bufferSize); AudioFormat format = new AudioFormat(8000, 16, 2, true, true); DataLine.Info info = new DataLine.Info(SourceDataLine.class, format); try { line = (SourceDataLine) AudioSystem.getLine(info); line.open(format, 10240); } catch (LineUnavailableException e) { e.printStackTrace(); } } public void run() { while (isStart && !thread.isInterrupted()) { byte[] data = super.receive(); line.write(data, 0, data.length);//将data的数据转化为音频 } } public void start() { if (thread == null || !thread.isAlive()) { thread = new Thread(this); line.start(); isStart = true; contect(); thread.start(); } } public void stop() { thread.interrupt(); line.stop(); isStart = false; close(); } }
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package n.audio.chart; import javax.swing.JTextArea; /** * @author Hello 把那几个sender、receiver又包装了一下,供GUI调用 */ public class Chat { public static void main(String[] args) { Chat c = new Chat(null); c.soundStart(); } //private MessageReceiver massageReceiver; //private MessageSender massageSender; private final SoundReceiver soundReceiver; private final SoundSender soundSender; public Chat(JTextArea textArea) { //massageSender = new MessageSender("231.0.0.1",10000,textArea); //massageReceiver = new MessageReceiver("231.0.0.1",10000,1024,textArea); soundSender = new SoundSender("231.0.0.1", 10001, 1024); soundReceiver = new SoundReceiver("231.0.0.1", 10001, 1024); //massageReceiver.start(); } public void send(String message) { //massageSender.send(message); } public void soundStart() { soundReceiver.start(); soundSender.start(); } public void soundStop() { soundReceiver.stop(); soundSender.stop(); } }
标签:
原文地址:http://www.cnblogs.com/tanhao/p/4251390.html