Record voice with Java
Asked Answered
T

1

16

I want to record voice using a Java application; I guess this will be basically an applet that will run on client side. But I don't have any idea of how to do it... any ideas? Also, I want to play the recorded voice.

I have heard of Java Speech API. Any idea if it can help?

Tarkington answered 28/1, 2011 at 8:16 Comment(2)
take a look at: ditra.cs.umu.se/jmf2_0-guide-html/JMFCapturing.htmlVanitavanity
@Vanitavanity - too complext to understand :(Tarkington
D
9

I am late to the party, but here are the official docs on capturing audio: http://docs.oracle.com/javase/tutorial/sound/capturing.html

(And copied directly from the link above here is some sample code to do it:)

TargetDataLine line;
DataLine.Info info = new DataLine.Info(TargetDataLine.class,
                format); // format is an AudioFormat object
if (!AudioSystem.isLineSupported(info)) {
    // Handle the error ...

}
// Obtain and open the line.
try {
    line = (TargetDataLine) AudioSystem.getLine(info);
    line.open(format);
} catch (LineUnavailableException ex) {
    // Handle the error ...
}

// Assume that the TargetDataLine, line, has already
// been obtained and opened.
ByteArrayOutputStream out  = new ByteArrayOutputStream();
int numBytesRead;
byte[] data = new byte[line.getBufferSize() / 5];

// Begin audio capture.
line.start();

// Here, stopped is a global boolean set by another thread.
while (!stopped) {
    // Read the next chunk of data from the TargetDataLine.
    numBytesRead =  line.read(data, 0, data.length);
    // Save this chunk of data.
    out.write(data, 0, numBytesRead);
}
Danyelledanyette answered 23/9, 2012 at 3:3 Comment(2)
in your example I have an error in stopped line, can you help meGoffer
I did write that 4+ years ago... and I did copy it from that url, but I assume you're having compile errors? That stopped variable is essentially a placeholder for a variable you should set/create/maintain elsewhere to know whether or not you're supposed to have stopped recording. So it's very particular to your case.Danyelledanyette

© 2022 - 2024 — McMap. All rights reserved.