Voice Recorder

Just a suggestion: I need to record the voice by microphone, get the wave file and send to a db. I found (googling) a simple class to do this: the wave file is generated but the user interface doesn’t work and the recording continues…
I am italian, sorry for my english… the class is this:


package audiorecorder;

import java.io.*;
import javax.sound.sampled.*;
 
/**
 * A sample program is to demonstrate how to record sound in Java
 * author: www.codejava.net
 */
public class AudioRecorder {
    // record duration, in milliseconds
    static final long RECORD_TIME = 120000;  // 1 minute
 
    // path of the wav file
    File wavFile = new File("/tmp/prova.wav");
 
    // format of audio file
    AudioFileFormat.Type fileType = AudioFileFormat.Type.WAVE;
 
    // the line from which audio data is captured
    TargetDataLine line;
 
    /**
     * Defines an audio format
     */
    AudioFormat getAudioFormat() {
        float sampleRate = 16000;
        int sampleSizeInBits = 8;
        int channels = 2;
        boolean signed = true;
        boolean bigEndian = true;
        AudioFormat format = new AudioFormat(sampleRate, sampleSizeInBits,
                                             channels, signed, bigEndian);
        return format;
    }
 
    /**
     * Captures the sound and record into a WAV file
     */
    void start() {
        try {
            AudioFormat format = getAudioFormat();
            DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
 
            // checks if system supports the data line
            if (!AudioSystem.isLineSupported(info)) {
                System.out.println("Line not supported");
                System.exit(0);
            }
            line = (TargetDataLine) AudioSystem.getLine(info);
            line.open(format);
            line.start();   // start capturing
 
            System.out.println("Start capturing...");
 
            AudioInputStream ais = new AudioInputStream(line);
 
            System.out.println("Start recording...");
 
            // start recording
            AudioSystem.write(ais, fileType, wavFile);
 
        } catch (LineUnavailableException ex) {
            ex.printStackTrace();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }
 
    /**
     * Closes the target data line to finish capturing and recording
     */
    void finish() {
        line.stop();
        line.close();
        System.out.println("Finished");
    }
 
    /**
     * Entry to run the program
     */
    public static void main(String[] args) {
        final AudioRecorder recorder = new AudioRecorder();
 
        // creates a new thread that waits for a specified
        // of time before stopping
        Thread stopper = new Thread(new Runnable() {
            public void run() {
                try {
                    Thread.sleep(RECORD_TIME);
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
                recorder.finish();
            }
        });
 
        stopper.start();
 
        // start recording
        recorder.start();
    }
}

You are trying to record audio on the server.

If you really want to do it from a microphone connected to the server, you might need to do it in a background thread so that you don’t block the UI.

If you want to record audio on the client, search for “HTML5 record audio” or see e.g.
this intro
. Note that these are JavaScript APIs, you need to create a Vaadin widget that wraps these and transmits the data to the server. I haven’t seen any add-ons that would already do this, although I have seen a demonstration of a video chat application in Vaadin using WebRTC (if I remember correctly) sometime last year.