hi
i m new to java programming and i want to make a voice chat which access a sql server db and extract ip 's of all clients that every one log in by ip (on LAN) and the problem face me now is to broadcast voice to all these ip 's specially time is nearly out this is a task in my class
here is the code in two classes that send and recieve voice between two pc
thanks
package voicechat2;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
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.LineUnavailableException;
import javax.sound.sampled.Mixer;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.TargetDataLine;
public class server { //first execute server then player
ServerSocket MyService; // waits for requests to come in over the network
Socket clientSocket = null; //endpoint for communication between two machines
BufferedInputStream input;
TargetDataLine targetDataLine; //type of DataLine from which audio data can be read
BufferedOutputStream out;
AudioFormat audioFormat; //specifies a particular arrangement of data in a sound stream
SourceDataLine sourceDataLine; //is a data line to which data may be written
byte tempBuffer[] = new byte[10000];
boolean record=true;
server() throws LineUnavailableException{ // constructor
try {
audioFormat = getAudioFormat();
DataLine.Info dataLineInfo = new DataLine.Info( SourceDataLine.class,audioFormat);
sourceDataLine = (SourceDataLine) AudioSystem.getLine(dataLineInfo);
sourceDataLine.open(audioFormat);
sourceDataLine.start();
MyService = new ServerSocket(500);
clientSocket = MyService.accept();
captureAudio();
input = new BufferedInputStream(clientSocket.getInputStream());
out=new BufferedOutputStream(clientSocket.getOutputStream());
while(input.read(tempBuffer)!=-1){
sourceDataLine.write(tempBuffer,0,10000);
}
} catch (IOException e) {
e.printStackTrace();
}
}
private AudioFormat getAudioFormat(){
float sampleRate = 8000.0F; int sampleSizeBits = 16;
int channels = 1; boolean signed = true; boolean bigEndian = false;
return new AudioFormat(sampleRate,sampleSizeBits, channels, signed, bigEndian);
}
public static void main(String s[]) throws LineUnavailableException{
server s2=new server();
}
public void soundOff(){ //make sound off
record=false;
}
private void captureAudio() {
while(record!=false){
try {
Mixer.Info[] mixerInfo = AudioSystem.getMixerInfo();
System.out.println("Available mixers:");
for (int cnt = 0; cnt < mixerInfo.length; cnt++) {
System.out.println(mixerInfo[cnt].getName());
}
audioFormat = getAudioFormat();
DataLine.Info dataLineInfo = new DataLine.Info( TargetDataLine.class, audioFormat);
Mixer mixer = AudioSystem.getMixer(mixerInfo[3]); //audio device with one or more lines support synchronization When one line in a synchronized group is started or stopped, the other lines in the group automatically start or stop simultaneously
targetDataLine = (TargetDataLine) mixer.getLine(dataLineInfo);
targetDataLine.open(audioFormat);
targetDataLine.start();
Thread captureThread = new CaptureThread();
captureThread.start();
} catch (Exception e) {
System.out.println(e);
System.exit(0);
}
}
}
class CaptureThread extends Thread {
byte tempBuffer[] = new byte[10000];
public void run() {
try {
while (true) {
int cnt = targetDataLine.read(tempBuffer, 0,tempBuffer.length);
out.write(tempBuffer);
}
} catch (Exception e) {
System.out.println(e);
System.exit(0);
}
}
}
}
package voicechat2;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.Socket;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.Mixer;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.TargetDataLine;
public class player {
boolean stopCapture = false;
ByteArrayOutputStream byteArrayOut;
AudioFormat audioFormat;
TargetDataLine targetDataLine;
BufferedOutputStream out = null;
BufferedInputStream in = null;
Socket socket = null;
SourceDataLine sourceDataLine;
public static void main(String[] args) {
player p = new player();
p.captureAudio();
}
private void captureAudio() {
try {
socket = new Socket("10.0.0.1", 500); //can use port 6771 & 5004
out = new BufferedOutputStream(socket.getOutputStream());
in = new BufferedInputStream(socket.getInputStream());
Mixer.Info[] mixerInfo = AudioSystem.getMixerInfo();
System.out.println("Available mixers:");
for (int cnt = 0; cnt < mixerInfo.length; cnt++) {
System.out.println(mixerInfo[cnt].getName());
}
audioFormat = getAudioFormat();
DataLine.Info dataLineInfo = new DataLine.Info( TargetDataLine.class, audioFormat);
Mixer mixer = AudioSystem.getMixer(mixerInfo[3]); //audio device with one or more lines support synchronization When one line in a synchronized group is started or stopped, the other lines in the group automatically start or stop simultaneously
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);
System.exit(0);
}
}
class CaptureThread extends Thread {
byte tempBuffer[] = new byte[10000];
public void run() {
byteArrayOut = new ByteArrayOutputStream();
stopCapture = false;
try {
while (stopCapture!=true) {
int cnt = targetDataLine.read(tempBuffer, 0,tempBuffer.length);
out.write(tempBuffer);
if (cnt > 0) {
byteArrayOut.write(tempBuffer, 0, cnt);
}
}
byteArrayOut.close();
} catch (Exception e) {
System.out.println(e);
System.exit(0);
}
}
}
public void mute(){ //makes sound mute not heared by collaborator
stopCapture = true;
}
private AudioFormat getAudioFormat() {
float sampleRate = 8000.0F; int sampleSizeInBits = 16;
int channels = 1; boolean signed = true; boolean bigEndian = false;
return new AudioFormat(sampleRate, sampleSizeInBits, channels, signed,bigEndian);
}
class PlayThread extends Thread {
byte tempBuffer[] = new byte[10000];
public void run() {
try {
while (in.read(tempBuffer) != -1) {
sourceDataLine.write(tempBuffer, 0, 10000);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}