dcsimg
applet recording a voice message and uploading to the server
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   matteo_gioia
Posted On:   Friday, March 17, 2006 09:42 AM

Hi all, I am looking for an example of an applet wich take the mic input, record an mp3 or wav file and upload it on a server. Here is some code I found in internet but I cannot make it work. Thankoyu very much in advance. matteo_gioia@yahoo.com ================ The applet code =============== /* * RecApplet.java * * Created on September 13, 2005, 10:05 AM */ /** * * @author Ravinder */ import javax.sound.sampled.*; import java.io.*; import java.net.*; import java.util.*; // // public class RecApplet extends javax.swing.JApplet implements Runnable { /** Init   More>>

Hi all,
I am looking for an example of an applet wich take the mic input, record an mp3 or wav file and upload it on a server.
Here is some code I found in internet but I cannot make it work.
Thankoyu very much in advance.

matteo_gioia@yahoo.com


================ The applet code ===============


/*
* RecApplet.java
*
* Created on September 13, 2005, 10:05 AM
*/

/**
*
* @author Ravinder
*/
import javax.sound.sampled.*;
import java.io.*;
import java.net.*;
import java.util.*;
//
//
public class RecApplet extends javax.swing.JApplet implements Runnable {

/** Initializes the applet RecApplet */
public void init() {
initComponents();
connect();
}
public void connect() {
//Initialize networking stuff.
String host = getCodeBase().getHost();
try
{
socket = new Socket(host,5555);
if (socket==null) {
System.out.println("Error in Socket creation");
System.exit(1);
}
outStrm = socket.getOutputStream();
inStrm = socket.getInputStream();
}
catch (Exception e)
{
}
}
private void initComponents() {//GEN-BEGIN:initComponents
java.awt.GridBagConstraints gridBagConstraints;

jLabel1 = new javax.swing.JLabel();
jb_record = new javax.swing.JButton();
jb_stop = new javax.swing.JButton();
jb_submit = new javax.swing.JButton();

getContentPane().setLayout(new java.awt.GridBagLayout());

jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel1.setText("jLabel1");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 0;
gridBagConstraints.gridwidth = 3;
gridBagConstraints.gridheight = 3;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.insets = new java.awt.Insets(7, 0, 7, 0);
getContentPane().add(jLabel1, gridBagConstraints);

jb_record.setText("jb_record");
jb_record.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jb_recordActionPerformed(evt);
}
});

gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 3;
gridBagConstraints.insets = new java.awt.Insets(7, 0, 7, 0);
getContentPane().add(jb_record, gridBagConstraints);

jb_stop.setText("Stop");
jb_stop.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jb_stopActionPerformed(evt);
}
});

gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 3;
gridBagConstraints.gridy = 3;
gridBagConstraints.insets = new java.awt.Insets(7, 0, 7, 0);
getContentPane().add(jb_stop, gridBagConstraints);

jb_submit.setText("Submit");
jb_submit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jb_submitActionPerformed(evt);
}
});

gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 2;
gridBagConstraints.gridy = 5;
gridBagConstraints.insets = new java.awt.Insets(7, 0, 7, 0);
getContentPane().add(jb_submit, gridBagConstraints);

}//GEN-END:initComponents

private void jb_submitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jb_submitActionPerformed
// TODO add your handling code here:
startRecording();
}//GEN-LAST:event_jb_submitActionPerformed

private void jb_stopActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jb_stopActionPerformed
// TODO add your handling code here:
stopRecording();
}//GEN-LAST:event_jb_stopActionPerformed

private void jb_recordActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jb_recordActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_jb_recordActionPerformed

public void startRecording() {
DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
if(!AudioSystem.isLineSupported(info)) {
System.out.println("Line not supported" + info);
jb_record.setEnabled(false);
return;
}
try {
mike = (TargetDataLine)AudioSystem.getLine(info);
mike.open(format, mike.getBufferSize());
}
catch(LineUnavailableException e) {
System.out.println("Line not available" + e);
jb_record.setEnabled(false);
return;
}
thread = new Thread(this);
thread.start();
}
public void stopRecording() {
mike.stop();
mike.close();
}
public void run() {
System.out.println("Inside run");
AudioInputStream sound = new AudioInputStream(mike);
mike.start();
try {
AudioSystem.write(sound, fileType, outStrm);
}
catch(IOException e) {
System.out.println(e);
}
}

Socket socket;
OutputStream outStrm;
InputStream inStrm;
int dataPort;
String host;
Thread thread;
boolean DEBUG = false;
private TargetDataLine mike;
private AudioFileFormat.Type fileType = AudioFileFormat.Type.WAVE;
final int mono = 1;
private AudioFormat format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 16000, 16, mono, 2, 16000, true);
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JLabel jLabel1;
private javax.swing.JButton jb_record;
private javax.swing.JButton jb_stop;
private javax.swing.JButton jb_submit;
// End of variables declaration//GEN-END:variables

}



================ The server code ===============


import java.io.*;
import java.net.*;
public class MyServer
{
ServerSocket srv;
Socket socket;
public MyServer() {
init();
}
private void init() {
try {
int port = 5555;
srv = new ServerSocket(port);
System.out.println("Running Server Socket on port " + port);
// Wait for connection from client.
} catch (IOException e) {
System.out.println("Could Not Create Server Socket");
}
int i=0;
while (i <5) { //put the condition as true to run the server infinitely
try {
socket = srv.accept();
System.out.println("Got a Client Request ....."+socket);
System.out.println("Processing Request ....");
if (processRequest()) System.out.println("Success");
i++;
}
catch (IOException ex) {
ex.printStackTrace();
System.out.println("There Was an Exception when trying to connect to client");
break;
}
}
}
private boolean processRequest() {
System.out.println("Getting The Wave File .....");
File f = getWavFile();
String str=null;
if (f==null) return false;
System.out.println("Wav file recieved successfully....");
System.out.println("Processing Search .....");
str = processSearch(f);

if (str==null) return false;
System.out.println("Search Process was successfull.....");
System.out.println("Sending Response.....");
return sendResponse(str);
}
private File getWavFile() {
File f=null;
try {
f = new File("./temp.wav");
if (f.exists()) f.delete();
System.out.println("created a temp file ....blocked");
InputStream is = socket.getInputStream();
System.out.println("successfully created input stream ....");
byte[] buf = new byte[1024];
FileOutputStream fos = new FileOutputStream(f);
int n=0;
System.out.println("Reading ....");
while ((n=is.read(buf))>0) {
System.out.println("read "+ n+ " bytes");
fos.write(buf,0,n);
System.out.println("wrote "+ n+ " bytes");
if (n <1024) break;
}
System.out.println("Finished Reading ....");
fos.close();

} catch (IOException e) {
System.out.println("Failed In either reading from socket or writing to file");
return null;
}
return f;
}
private String processSearch(File f) {
String result="";
try {
// Execute command
System.out.println("Creating a process...");
String[] commands = {"../parameters/z.csh"};
Process child = Runtime.getRuntime().exec(commands);
System.out.println("Executed Command");
// Get the input stream and read from it
BufferedReader rd1 = new BufferedReader(new
InputStreamReader(child.getInputStream()));
// InputStream in = child.getInputStream();*/
System.out.println("Reading Output...");
String s;
while ((s=rd1.readLine()) != null) {
System.out.println(s);
}
rd1.close();
BufferedReader rd2 = new BufferedReader(new
FileReader("../parameters/interface1/ansinp.txt"));
// System.out.println("Got The input Stream");

while ((s = rd2.readLine()) != null) {
System.out.println(s);
result+=s+"
";
}
rd2.close();
} catch (IOException e) {
System.out.println("got an exception " +e.getMessage());
return "Failed in Executing The Command";
}
return result+"\0";
}
private boolean sendResponse(String str) {
try {
BufferedWriter wr = new BufferedWriter(new
OutputStreamWriter(socket.getOutputStream()));
// System.out.println("Opened Write Stream ....");
wr.write(str);
wr.flush();
wr.close();
} catch (IOException e) {
System.out.println("Things not going as planned");
return false;
}
return true;
}
public static void main(String[] args) {
new MyServer();
System.out.println("Hello World!");
}
}



================ The html code ===============




code="RecApplet.class"
width=300
height=350
name="RecApplet" >

   <<Less
About | Sitemap | Contact