I'm having trouble with my LAN VOICE CHAT project.
I've made the program for the LAN Voice Chat, but I am not able to implement it along with the swing GUI that I created.
Here is the Server Module:
////*Execute LVC1 first on one computer, then execute LVC2 on the second computer by passing the IP of the first computer as a comand line argument
*////
import java.io.*;
import java.net.*;
import javax.sound.sampled.*;
public class LVC1
{
ServerSocket MyService;
Socket clientSocket = null;
BufferedInputStream input;
TargetDataLine targetDataLine;
BufferedOutputStream out;
ByteArrayOutputStream byteArrayOutputStream;
AudioFormat audioFormat;
SourceDataLine sourceDataLine;
byte tempBuffer[] = new byte[10000];
LVC1() throws LineUnavailableException
{
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(1987);
clientSocket = MyService.accept();
System.out.println("Accepted");
ScaptureAudio();
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 sampleSizeInBits = 16;
int channels = 1;
boolean signed = true;
boolean bigEndian = false;
return new AudioFormat(sampleRate, sampleSizeInBits, channels, signed, bigEndian);
}
public static void main(String s[]) throws LineUnavailableException
{
LVC1 lvc1=new LVC1();
}
private void ScaptureAudio()
{
try
{
Mixer.Info[] mixerInfo = AudioSystem.getMixerInfo();
audioFormat = getAudioFormat();
DataLine.Info dataLineInfo = new DataLine.Info(TargetDataLine.class, audioFormat);
Mixer mixer = AudioSystem.getMixer(mixerInfo[3]);
targetDataLine = (TargetDataLine) mixer.getLine(dataLineInfo);
targetDataLine.open(audioFormat);
targetDataLine.start();
Thread ScaptureThread = new SCaptureThread();
ScaptureThread.start();
}
catch (Exception e)
{
System.out.println(e);
System.exit(0);
}
}
class SCaptureThread 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);
}
}
}
}This is the Client Module:////*Execute LVC1 first on one computer, then execute LVC2 on the second computer by passing the IP of the first computer as a comand line argument
*////
import java.io.*;
import java.net.*;
import javax.sound.sampled.*;
public class LVC2
{
boolean stopCapture = false;
ByteArrayOutputStream byteArrayOutputStream;
AudioFormat audioFormat;
TargetDataLine targetDataLine;
AudioInputStream audioInputStream;
BufferedOutputStream out = null;
BufferedInputStream in = null;
Socket sock = null;
SourceDataLine sourceDataLine;
public static void main(String[] args)
{
LVC2 lvcc = new LVC2();
lvcc.captureAudio(args[0]);
}
private void captureAudio(String s)
{
try
{
sock = new Socket(s, 1987);
out = new BufferedOutputStream(sock.getOutputStream());
in = new BufferedInputStream(sock.getInputStream());
Mixer.Info[] mixerInfo = AudioSystem.getMixerInfo();
audioFormat = getAudioFormat();
DataLine.Info dataLineInfo = new DataLine.Info(TargetDataLine.class, audioFormat);
Mixer mixer = AudioSystem.getMixer(mixerInfo[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);
System.exit(0);
}
}
class CaptureThread extends Thread
{
byte tempBuffer[] = new byte[10000];
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);
System.exit(0);
}
}
}
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();
}
}
}
}And this is the swing GUI:import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/*<applet code = "LVC" width =350 height =150 ></applet>*/
public class LVC extends JApplet implements ActionListener
{
JLabel ipText;
JTextField ipAddress;
JButton connectBtn;
JButton talkBtn;
JButton disconnectBtn;
public void init()
{
Container contentPane = getContentPane();
contentPane.setLayout(new FlowLayout());
ipText = new JLabel("IP Address ");
add(ipText);
ipAddress = new JTextField (20);
contentPane.add(ipAddress);
ImageIcon connectImg = new ImageIcon("connect.png");
connectBtn = new JButton(connectImg);
connectBtn.setActionCommand("Connecting . . . ");
connectBtn.addActionListener(this);
contentPane.add(connectBtn);
ImageIcon talkImg = new ImageIcon("phone.png");
talkBtn = new JButton(talkImg);
talkBtn.setActionCommand("Initializing Voice Input . . .");
talkBtn.addActionListener(this);
contentPane.add(talkBtn);
ImageIcon disconnectImg = new ImageIcon("endcall.gif");
disconnectBtn = new JButton(disconnectImg);
disconnectBtn.setActionCommand("Disconnected . . . ");
disconnectBtn.addActionListener(this);
contentPane.add(disconnectBtn);
}
public void actionPerformed(ActionEvent p)
{
showStatus(p.getActionCommand());
}
} LVC1 and LVC2 work fine together. But I'm not able to get the socket connections accepted between the server and client when I use them with the GUI.Maybe theres is some compatibility issue with swing? And we'll have to use frames? Just a thought... not sure
I am sorry but, I have not included the code that I came up with by combining LVC1 and LVC2 with the GUI module LVC cuz I'm not able to find it. I'll add them as soon as I do.
I was hoping someone could help me in the meantime.
I want to take the IP Address of the server as the input from the Client module's IP Address text field. On the Server side, the status bar should show 'Connection accepted'.
When the Connect button is pressed, the client will attemp to connect to the server.
When I press Disconnect button, the sockets will close.
If its too tedious, we could combine the Connect and Talk buttons into one. Which ever is easier.
I'm really sorry, I couldn't add the code I had came up with and its presumtuous of me to assume that somone will take out their time and actually do this. Debugging would've been much easier.
But having said that, help would be greatly appreciated.
I have to submit this by 9th June. :@


Sign In
Create Account

Back to top









