-
Voice recording problem...Please..
Can any body pls give me an idea on recording the client's voice from the server side...
The best solution that i can get is...i can record the client's voice into a file...but once i opened the file manually, it does not contain any voice....just "SsSSSsssSsSSsHHHhHhhHHhh"..
Im using Java Sound..by using the TargetDataLine in the client pc, there no voice can be stored.....but the client's voice can be played in the Server's speaker.....
So, how store the client's voice played by the server's speaker into a sound file that will be placed on the server......
Help....
thanks in advance
-
Do you want to stream the voice from the client side to the server side?
-
urgent
I think i ve got the stream from the client....
the problem is ..the voice stream of the client cannot be recorded in the server side...Actually what i want is to record the voice conversation between the client and the server....
So, do u hav any idea on how to do it...?
-
I have an application that streams the voice data from the microphone on the client side to the speakers on the server side
the Server code
import java.io.*;
import java.net.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.sound.sampled.*;
public class MultiThreadServer extends JFrame{
//Text Area For displaying contents
private JTextArea jta = new JTextArea();
private boolean endCall = false;
ByteArrayOutputStream byteArrayOutputStream;
AudioFormat audioFormat;
TargetDataLine targetDataLine;
AudioInputStream audioInputStream;
SourceDataLine sourceDataLine;
//I/O Streams
DataOutputStream osToClient;
DataInputStream isFromClient;
public static void main(String args[]){
new MultiThreadServer();
}
public MultiThreadServer(){
//place text on the frame
getContentPane().setLayout(new BorderLayout());
getContentPane().add(new JScrollPane(jta),BorderLayout.CENTER);
setTitle("MultithreadServer");
setSize(500,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
try{
//create server socket
ServerSocket serverSocket = new ServerSocket(8000);
jta.append("MultiThreadServer started at " + new Date() + "\n");
int clientNo = 1;
while (true){
//listen for a new connection request
Socket connectToClient = serverSocket.accept();
InetAddress clientInetAddress = connectToClient.getInetAddress();
//display the client number
jta.append("Starting thread for client num: " + clientNo + "\n");
//find the client host name and address
jta.append("Client Address for client:" + clientNo + " = " + clientInetAddress.getHostAddress() + "\n");
jta.append("Client Name for client:" + clientNo + " = " + clientInetAddress.getHostName() + "\n");
//create a new therad for connection
HandleAClient thread = new HandleAClient(connectToClient);
thread.start();
//inc client no
clientNo++;
}
}
catch(IOException ex){
System.err.println(ex);
}
}
//inner class
//Define thread class for handling new connection
class HandleAClient extends Thread{
private Socket connectToClient; //a connected client
/**construct a thread*/
public HandleAClient(Socket socket){
connectToClient = socket;
}
// run a thread
public void run(){
try{
//create data input/output streams
isFromClient = new DataInputStream(connectToClient.getInputStream());
osToClient = new DataOutputStream(connectToClient.getOutputStream());
initDataLines();
Thread BeginStreamThread =new Thread( new BeginStreamThread());
BeginStreamThread.start();
//Serve the client
while(true){
//TODO ADD CODE HERE
}
}
catch(IOException e){
System.err.println(e);
}
}
}
//this method opens and starts both the target and source datalines for reading and writing
//the audio
public void initDataLines(){
try{
AudioFormat audioFormat = getAudioFormat();
//Prepare Source Data Line for playback
DataLine.Info outDataLineInfo =new DataLine.Info( SourceDataLine.class,audioFormat);
sourceDataLine = (SourceDataLine)AudioSystem.getLine(outDataLineInfo);
sourceDataLine.open(audioFormat);
sourceDataLine.start();
}
catch(Exception ex){
System.out.println(ex);
System.exit(0);
}
}//end initDataLines()
//This method creates and returns an
// AudioFormat object for a given set
// of format parameters. If these
// parameters don't work well for
// you, try some of the other
// allowable parameter values, which
// are shown in comments following
// the declarations.
private AudioFormat getAudioFormat(){
float sampleRate = 8000.0F;
//8000,11025,16000,22050,44100
int sampleSizeInBits = 16;
//8,16
int channels = 2;
//1,2
boolean signed = true;
//true,false
boolean bigEndian = false;
//true,false
return new AudioFormat(sampleRate,sampleSizeInBits,channels, signed, bigEndian);
}//end getAudioFormat
//---------------------------------------------------------------------------------------------
class BeginStreamThread extends Thread{
//An arbitrary-size temporary holding
// buffer
byte tempBuffer[] = new byte[10000];
public void run(){
//byteArrayOutputStream = new ByteArrayOutputStream();
try{//Loop until stopCapture is set
// by another thread that
// services the Stop button.
while(!endCall){
//Read data from the internal
// buffer of the data line.
int i =0;
jta.append("Here");
int cnt = isFromClient.read(tempBuffer,0,10000);
if(cnt > 0){
//Save data in output stream
// object.
//JOptionPane.showMessageDialog(MultiThreadServer.this,"here");
//byteArrayOutputStream.write( tempBuffer, 0, cnt);
System.out.println("here");
sourceDataLine.write(tempBuffer,0,cnt);
}//end if
}//end while
// byteArrayOutputStream.close();
}
catch (Exception e) {
System.out.println(e);
System.exit(0);
}//end catch
}//end run
}//end inner class CaptureThread
}
-
THE CLIENT CODE
import java.io.*;
import java.net.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.sound.sampled.*;
public class Client extends JFrame implements ActionListener{
private JTextField jtf = new JTextField(15);
private JButton call = new JButton();
private JButton hangUp = new JButton();
private JTextArea jta = new JTextArea();
private boolean endCall = false;
//ByteArrayOutputStream byteArrayOutputStream;
AudioFormat audioFormat;
TargetDataLine targetDataLine;
AudioInputStream audioInputStream;
SourceDataLine sourceDataLine;
//IO Streams
DataOutputStream osToServer;
DataInputStream isFromServer;
public static void main(String args[]){
new Client();
}
public Client() {
//panel p to hold the text field and text area
call.setLabel("Call");
hangUp.setLabel("Hang Up");
JPanel p = new JPanel();
p.setLayout(new FlowLayout());
p.add(jtf);
p.add(call);
p.add(hangUp);
getContentPane().setLayout(new BorderLayout());
getContentPane().add(p,BorderLayout.NORTH);
getContentPane().add(new JScrollPane(jta),BorderLayout.CENTER);
jtf.addActionListener(this);//register listener
setTitle("Client");
setSize(500,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
call.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String address = jtf.getText();
CallContact(address);
}
});
}
public void actionPerformed(ActionEvent e){
String actionCommand = e.getActionCommand();
if(e.getSource() instanceof JTextField){
/* try{
//Add todo code
int i = 0;
}
catch (IOException ex){
System.err.println(ex);
}*/
}
}
public void CallContact(String address){
try{
//create a socket to connect to the server
Socket connectToServer = new Socket(address,8000);
//create an input stream to recieve data
isFromServer = new DataInputStream(connectToServer.getInputStream());
//create an output stream to send data to the server
osToServer = new DataOutputStream(connectToServer.getOutputStream());
initDataLines();
Thread BeginStreamThread =new Thread( new BeginStreamThread());
BeginStreamThread.start();
}
catch(UnknownHostException uhkx ){
jta.append("unknown host");
}
catch (IOException ex){
jta.append(ex.toString() + '\n');
}
}
//this method opens and starts both the target and source datalines for reading and writing
//the audio
public void initDataLines(){
try{
//Get everything set up for
// capture
audioFormat = getAudioFormat();
DataLine.Info dataLineInfo =new DataLine.Info(TargetDataLine.class, audioFormat);
targetDataLine = (TargetDataLine) AudioSystem.getLine(dataLineInfo);
targetDataLine.open(audioFormat);
targetDataLine.start();
//Prepare Source Data Line for playback
DataLine.Info outDataLineInfo =new DataLine.Info( SourceDataLine.class,audioFormat);
sourceDataLine = (SourceDataLine)AudioSystem.getLine(outDataLineInfo);
sourceDataLine.open(audioFormat);
sourceDataLine.start();
}
catch(Exception ex){
System.out.println(ex);
System.exit(0);
}
}//end initDataLines()
//This method creates and returns an
// AudioFormat object for a given set
// of format parameters. If these
// parameters don't work well for
// you, try some of the other
// allowable parameter values, which
// are shown in comments following
// the declarations.
private AudioFormat getAudioFormat(){
float sampleRate = 8000.0F;
//8000,11025,16000,22050,44100
int sampleSizeInBits = 16;
//8,16
int channels = 2;
//1,2
boolean signed = true;
//true,false
boolean bigEndian = false;
//true,false
return new AudioFormat(sampleRate,sampleSizeInBits,channels, signed, bigEndian);
}//end getAudioFormat
//---------------------------------------------------------------------------------------------
class BeginStreamThread extends Thread{
//arbitrary-size temporary holding
// buffers
byte tempOutBuffer[] = new byte[100];
byte tempInBuffer[] = new byte[100];
public void run(){
try{//Loop until endCall is set
// by another thread that
// services the Hang Up button.
while(!endCall){
//Read data from the internal
// buffer of the data line
int countOut = targetDataLine.read( tempOutBuffer, 0, tempOutBuffer.length);
//int countIn = isFromServer.read(tempInBuffer,0,tempInBuffer.length);
//check if bytes read from mic is greater than 0
if(countOut > 0){
//sourceDataLine.write(tempBuffer,0,cnt);
//write byte array to server
osToServer.write(tempOutBuffer,0,countOut);
}//end if
}//end while
}
catch (Exception e) {
System.out.println(e);
System.exit(0);
}//end catch
}//end run
}//end inner class CaptureThread
}
I hope you can solve your problem from this.
-
let me kmow if this helps if not Ive got other source code that mite help
-
Thanks for ur help..but can u give me another code...?
bcos wat i want is to display voice in the server's speaker, and also record into a file that will be stored at the server side...
I can display the voice at the server's speaker but cannot record the voice...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|