UDP
发送端代码:
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.SourceDataLine;
public class AudioSender {
private DatagramSocket socket;
private InetAddress address;
private int port;
private boolean running;
private SourceDataLine line;
public AudioSender(String ipAddress, int port) {
try {
this.socket = new DatagramSocket();
this.address = InetAddress.getByName(ipAddress);
this.port = port;
this.running = true;
} catch (Exception e) {
e.printStackTrace();
}
}
public void start() {
try {
AudioFormat format = new AudioFormat(8000.0f, 16, 1, true, true);
DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
this.line = (SourceDataLine) AudioSystem.getLine(info);
this.line.open(format);
this.line.start();
byte[] buffer = new byte[1024];
DatagramPacket packet = new DatagramPacket(buffer, buffer.length, this.address, this.port);
while (this.running) {
int count = this.line.read(buffer, 0, buffer.length);
if (count > 0) {
packet.setData(buffer);
packet.setLength(count);
this.socket.send(packet);
}
}
this.line.drain();
this.line.stop();
this.socket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void stop() {
this.running = false;
}
public static void main(String[] args) {
AudioSender sender = new AudioSender("127.0.0.1", 8000);
sender.start();
}
}
接收端代码:
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.TargetDataLine;
public class AudioReceiver {
private DatagramSocket socket;
private int port;
private boolean running;
private TargetDataLine line;
public AudioReceiver(int port) {
try {
this.socket = new DatagramSocket(port);
this.port = port;
this.running = true;
} catch (Exception e) {
e.printStackTrace();
}
}
public void start() {
try {
AudioFormat format = new AudioFormat(8000.0f, 16, 1, true, true);
DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
this.line = (TargetDataLine) AudioSystem.getLine(info);
this.line.open(format);
this.line.start();
byte[] buffer = new byte[1024];
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
while (this.running) {
this.socket.receive(packet);
byte[] data = packet.getData();
int length = packet.getLength();
if (length > 0) {
this.line.write(data, 0, length);
}
}
this.line.drain();
this.line.stop();
this.socket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void stop() {
this.running = false;
}
public static void main(String[] args) {
AudioReceiver receiver = new AudioReceiver(8000);
receiver.start();
}
}
这段代码实现了一个简单的音频发送器和接收器,可以把麦克风输入的音频数据通过UDP协议发送出去,并在接收端播放出来。其中,AudioFormat 和 DataLine.Info 是用于配置音频输入输出的参数,具体可以根据实际需求进行调整。start() 方法中,发送端通过读取麦克风输入的音频数据,将其存储到 DatagramPacket 对象中,并通过 DatagramSocket 发送出去;接收端通过 DatagramSocket 接收数据,并将数据写入到音频输出设备中播放。stop() 方法用于停止发送和接收数据。注意,这段代码中没有对数据进行加密和解密,实际应用中需要考虑安全性问题。
TCP
发送端代码:
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.SourceDataLine;
public class AudioSender {
private Socket socket;
private OutputStream outputStream;
private boolean running;
private SourceDataLine line;
public AudioSender(String ipAddress, int port) {
try {
this.socket = new Socket(ipAddress, port);
this.outputStream = this.socket.getOutputStream();
this.running = true;
} catch (Exception e) {
e.printStackTrace();
}
}
public void start() {
try {
AudioFormat format = new AudioFormat(8000.0f, 16, 1, true, true);
DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
this.line = (SourceDataLine) AudioSystem.getLine(info);
this.line.open(format);
this.line.start();
byte[] buffer = new byte[1024];
while (this.running) {
int count = this.line.read(buffer, 0, buffer.length);
if (count > 0) {
this.outputStream.write(buffer, 0, count);
}
}
this.line.drain();
this.line.stop();
this.outputStream.close();
this.socket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void stop() {
this.running = false;
}
public static void main(String[] args) {
AudioSender sender = new AudioSender("127.0.0.1", 8000);
sender.start();
}
}
接收端代码:
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.TargetDataLine;
public class AudioReceiver {
private ServerSocket serverSocket;
private Socket socket;
private InputStream inputStream;
private boolean running;
private TargetDataLine line;
public AudioReceiver(int port) {
try {
this.serverSocket = new ServerSocket(port);
this.socket = this.serverSocket.accept();
this.inputStream = this.socket.getInputStream();
this.running = true;
} catch (Exception e) {
e.printStackTrace();
}
}
public void start() {
try {
AudioFormat format = new AudioFormat(8000.0f, 16, 1, true, true);
DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
this.line = (TargetDataLine) AudioSystem.getLine(info);
this.line.open(format);
this.line.start();
byte[] buffer = new byte[1024];
while (this.running) {
int count = this.inputStream.read(buffer, 0, buffer.length);
if (count > 0) {
this.line.write(buffer, 0, count);
}
}
this.line.drain();
this.line.stop();
this.inputStream.close();
this.socket.close();
this.serverSocket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void stop() {
this.running = false;
}
public static void main(String[] args) {
AudioReceiver receiver = new AudioReceiver(8000);
receiver.start();
}
}
这段代码实现了一个简单的音频发送器和接收器,可以把麦克风输入的音频数据通过TCP协议发送出去,并在接收端播放出来。其中,AudioFormat 和 DataLine.Info 是用于配置音频输入输出的参数,具体可以根据实际需求进行调整。start() 方法中,发送端通过读取麦克风输入的音频数据,将其存储到 OutputStream 对象中,并通过 Socket 发送出去;接收端通过 ServerSocket 接收连接,获取 InputStream 对象,并将数据写入到音频输出设备中播放。stop() 方法用于停止发送和接收数据。注意,这段代码中没有对数据进行加密和解密,实际应用中需要考虑安全性问题。