• Post Reply Bookmark Topic Watch Topic
  • New Topic

voice chat  RSS feed

 
zohreh talebi
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Friends,

I want to write a web base chat application with voice.I did so many search on this topic ,unfortunatly i didn't find useful thing except this codes but i don't know weather it is good for me or not .
Can every one see and guide me or introduse some resources.I would be so glad if see your any ideas.




import java.io.Serializable;
import java.net.InetAddress;
import java.net.Socket;
import javax.swing.JOptionPane;

public class Sc implements Serializable
{
private static InetAddress host;
private static final int PORT = 1234;
public static Socket link;
public String hostName;
public Sc()
{
try{
String d=JOptionPane.showInputDialog(null,"");
hostName =String.valueOf(d) ;
if(hostName==null)
hostName="localhost";
host = InetAddress.getByName(hostName);

link = new Socket(host,PORT);
}catch(Exception e)
{System.out.println("ERROR in sc"+e);
}
JOptionPane.showMessageDialog(null,+PORT+" "+host);
if( PORT==1234)
{ JOptionPane.showMessageDialog(null,"start voice recording");
MicReader m = new MicReader();

try {

m.startRecording();

}catch (Exception e)
{JOptionPane.showMessageDialog(null,"error in SC "+e);
}
}}

public static void main(String args[])
{

Sc t=new Sc();
}}

-----------------------------------------------------------------------------


import java.io.DataInputStream;
import java.io.IOException;
//import java.io.RandomAccessFile;
import java.io.Serializable;
import java.net.ServerSocket;
import java.net.Socket;

import javax.swing.JOptionPane;
public class Ss implements Serializable
{


public static ServerSocket servSocket;
private static final int PORT = 8080;

public static Socket client;
public Ss() throws IOException
{
try {
servSocket = new ServerSocket(PORT);
JOptionPane.showMessageDialog(null," Set Up Port");
} catch (IOException ioe) {

JOptionPane.showMessageDialog(null,"Unable To Set Up Port");
System.exit(1);
}

try
{
client = servSocket.accept();
// RandomAccessFile fout=new RandomAccessFile("Voice1.wav","rw");

int option;
do
{



AudioPlayerTest a=new AudioPlayerTest();
JOptionPane.showMessageDialog(null,"called ap");

a.playFile();

}while(true);

}catch(Exception e)
{
JOptionPane.showMessageDialog(null,"SS"+e);
}

}


public static void main(String args[])
{
try
{

Ss t=new Ss();
}catch(Exception e)
{JOptionPane.showMessageDialog(null,"main exit");
}
}
}

-----------------------------------------------------------------------------
import java.io.DataInputStream;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.SourceDataLine;

public class AudioPlayerTest {

// String audioFileName;
SourceDataLine line;
// DataInputStream in;
public AudioPlayerTest() {
// audioFileName = fName;
// in1=in;
try {
AudioFormat aFormat =
new AudioFormat(
AudioFormat.Encoding.PCM_SIGNED,
44100.0f,
16,
2,
4,
44100.0f,
false);
DataLine.Info info =
new DataLine.Info(SourceDataLine.class, aFormat);
line = (SourceDataLine) AudioSystem.getLine(info);

line.open(aFormat);

}
catch (Exception e) {
System.out.println("Line Exception" + e);
}}

public void playFile() {
byte[] data = new byte[1024];
try {
while(true)
{
DataInputStream In= new DataInputStream(Ss.client.getInputStream());
In.read(data,0,1024);
if(data.equals(null))
System.out.println("no data read");
else
{

line.start();
// JOptionPane.showMessageDialog(null,"satred");

// JOptionPane.showMessageDialog(null,"read");
System.out.println("read"+data);

line.write(data, 0, 1024);
}
// JOptionPane.showMessageDialog(null,"written");
/*line.drain();
line.stop();
line.close();
line = null;*/
}
} catch (Exception e) {
System.out.println("Line Exception" + e);
}

}
}

----------------------------------------------------------------------------
import java.io.DataOutputStream;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.TargetDataLine;

public class MicReader extends Thread {

String audioFileName;
AudioInputStream audioLine;
// FileOutputStream fout;
// RandomAccessFile outputFile;
// boolean isOver = false;
TargetDataLine line;
public MicReader() {
// audioFileName = fName;
try {
// File outputFile = new File("voice.wav","rw");
// outputFile=new RandomAccessFile("audioFileName","rws");
AudioFormat aFormat =
new AudioFormat(
AudioFormat.Encoding.PCM_SIGNED,
44100.0f,
16,
2,
4,
44100.0f,
false);
DataLine.Info info =
new DataLine.Info(TargetDataLine.class, aFormat);
line = (TargetDataLine) AudioSystem.getLine(info);
audioLine = new AudioInputStream(line);
line.open(aFormat);
// FileOutputStream output=(FileOutputStream)outputFile;
// System.out.println("Line Initialized...");
// fout = new FileOutputStream(outputFile);
// outputFile=new RandomAccessFile("audioFileName","w");
// fout=new FileOutputStream(outputFile);

} catch (Exception e) {
System.out.println("Exception " + e);
System.exit(0);

}

}

public void startRecording() {
line.start();
start();

}

public void stopRecording() {
// isOver = true;
}

public void run() {
byte[] data = new byte[1024];

while (true) {

try {
DataOutputStream out=new DataOutputStream(Sc.link.getOutputStream());
int bytes=line.read(data,0,1024);
out.write(data,0,bytes);
out.flush();

} catch (Exception e1) {
System.out.println("micreader" + e1);
}
}


}
}


Thanks
Zohreh
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basically you have dumped a bunch of raw code into the wrong forum (Note the JSP in the forum title) and expect people to do what?? Analyze the code for you?? People should only show code when there is a code specific problem, and then use code formatting so it is readable.
Bill
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!