我无法将麦克风语音流传输到其他客户端。
我创建了一个Java应用程序,该应用程序可以使用tcp连接进行本地网络中的聊天和语音流,在这种情况下,每个客户端的聊天功能都可以正常工作,但是语音流是一个问题。只有两个客户端可以使用语音功能,而并非全部。基本思想是,启动MD类的任何人都将能够录制语音并将其流传输到不同的客户端,并且所有连接的客户端将仅收听语音(不回复)。
我是Java编程的初学者,所以我不知道这个问题。如果有人能在这方面取悦我。预先谢谢你。
// MD.java
public class MD
{
ServerSocket MyService;
Socket clientSocket = null;
InputStream input;
TargetDataLine targetDataLine;
OutputStream out;
AudioFormat audioFormat;
SourceDataLine sourceDataLine;
int Size = 10000;
byte tempBuffer[] = new byte[Size];
static Mixer.Info[] mixerInfo = AudioSystem.getMixerInfo();
MD() throws LineUnavailableException, HeadlessException, UnknownHostException, InterruptedException {
/*JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("Network Phone");
JLabel label = new JLabel("Server ip: "+InetAddress.getLocalHost().getHostAddress(), JLabel.CENTER );
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
*/
// frame.pack();
// frame.show();
try {
Mixer mixer_ = AudioSystem.getMixer(mixerInfo[1]); // Select Available Hardware Devices for the speaker, for my Notebook it is number 1
audioFormat = getAudioFormat();
DataLine.Info dataLineInfo = new DataLine.Info(SourceDataLine.class, audioFormat);
sourceDataLine = (SourceDataLine) AudioSystem.getLine(dataLineInfo);
sourceDataLine.open(audioFormat);
sourceDataLine.start();
MyService = new ServerSocket(5000);
clientSocket = MyService.accept();
captureAudio();
input = new BufferedInputStream(clientSocket.getInputStream());
out = new BufferedOutputStream(clientSocket.getOutputStream());
Thread readbffr = new Thread(new Runnable(){
public void run(){
try{
while (input.read(tempBuffer) != -1) {
sourceDataLine.write(tempBuffer, 0, Size);
}
}
catch(IOException ex){
//Handle This Too
ex.printStackTrace();
}
};
});
readbffr.start();
} catch (IOException e) {
e.printStackTrace();
}
}
private AudioFormat getAudioFormat() {
float sampleRate = 8000.0F;
int sampleSizeInBits = 16;
int channels = 2;
boolean signed = true;
boolean bigEndian = false;
return new AudioFormat(
sampleRate,
sampleSizeInBits,
channels,
signed,
bigEndian);
}
public static void main(String[] args) throws LineUnavailableException, UnknownHostException, HeadlessException, InterruptedException {
MD s2 = new MD();
}
private void captureAudio() {
try {
audioFormat = getAudioFormat();
DataLine.Info dataLineInfo = new DataLine.Info(
TargetDataLine.class, audioFormat);
Mixer mixer = null;
System.out.println("Server Ip Address "+InetAddress.getLocalHost().getHostAddress());
System.out.println("Available Hardware Devices:");
for (int cnt = 0; cnt < mixerInfo.length; cnt++) {
mixer = AudioSystem.getMixer(mixerInfo[3]); // Select Available Hardware Devices for the micro, for my Notebook it is number 3
if (mixer.isLineSupported(dataLineInfo)) {
System.out.println(cnt+":"+mixerInfo[cnt].getName());
targetDataLine = (TargetDataLine) mixer.getLine(dataLineInfo);
}
}
targetDataLine.open(audioFormat);
targetDataLine.start();
Thread captureThread = new CaptureThread();
captureThread.start();
} catch (Exception e) {
System.out.println(e);
}
}
class CaptureThread extends Thread {
byte tempBuffer[] = new byte[Size];
@Override
public void run() {
try {
while (true) {
int cnt = targetDataLine.read(tempBuffer, 0, tempBuffer.length);
out.write(tempBuffer);
out.flush();
}
} catch (Exception e) {
System.out.println(e);
}
}
}}
// Employee.java
public class Employee {
boolean stopCapture = false;
ByteArrayOutputStream byteArrayOutputStream;
AudioFormat audioFormat;
TargetDataLine targetDataLine;
AudioInputStream audioInputStream;
BufferedOutputStream out = null;
BufferedInputStream in = null;
Socket sock = null;
SourceDataLine sourceDataLine;
/**
* Launch the application.
* @param args
*/
public static void main(String[] args) {
Employee i = new Employee();
i.captureAudio();
}
void captureAudio() {
try {
String IP =InetAddress.getLocalHost().getHostAddress();
sock = new Socket("192.168.1.1",5000);
out = new BufferedOutputStream(sock.getOutputStream());
in = new BufferedInputStream(sock.getInputStream());
Mixer.Info[] mixerInfo = AudioSystem.getMixerInfo();
System.out.println("Available Hardware devices:");
for (int cnt = 0; cnt < mixerInfo.length; cnt++) {
System.out.println(cnt+":"+mixerInfo[cnt].getName());
}
audioFormat = getAudioFormat();
/*
DataLine.Info dataLineInfo = new DataLine.Info(
TargetDataLine.class, audioFormat);
Mixer mixer = AudioSystem.getMixer(mixerInfo[3]); //Select Available Hardware Devices for the micro, for my Notebook it is number 3.
targetDataLine = (TargetDataLine) mixer.getLine(dataLineInfo);
targetDataLine.open(audioFormat);
targetDataLine.start();
Thread captureThread = new CaptureThread();
captureThread.start();
*/
DataLine.Info dataLineInfo1 = new DataLine.Info(
SourceDataLine.class, audioFormat);
sourceDataLine = (SourceDataLine) AudioSystem
.getLine(dataLineInfo1);
sourceDataLine.open(audioFormat);
sourceDataLine.start();
Thread playThread = new PlayThread();
playThread.start();
} catch (Exception e) {
System.out.println(e);
e.printStackTrace();
System.exit(0);
}
}
class CaptureThread extends Thread {
byte tempBuffer[] = new byte[10000];
@Override
public void run() {
byteArrayOutputStream = new ByteArrayOutputStream();
stopCapture = false;
try {
while (!stopCapture) {
int cnt = targetDataLine.read(tempBuffer, 0,
tempBuffer.length);
out.write(tempBuffer);
if (cnt > 0) {
byteArrayOutputStream.write(tempBuffer, 0, cnt);
}
}
byteArrayOutputStream.close();
} catch (Exception e) {
System.out.println(e);
e.printStackTrace();
System.exit(0);
}
}
}
private AudioFormat getAudioFormat() {
float sampleRate = 8000.0F;
int sampleSizeInBits = 16;
int channels = 2;
boolean signed = true;
boolean bigEndian = false;
return new AudioFormat(sampleRate, sampleSizeInBits, channels, signed,
bigEndian);
}
class PlayThread extends Thread {
byte tempBuffer[] = new byte[10000];
@Override
public void run() {
try {
while (in.read(tempBuffer) != -1) {
sourceDataLine.write(tempBuffer, 0, 10000);
}
sourceDataLine.drain();
sourceDataLine.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}