与Java录制语音

问题描述 投票:13回答:1

我想用一个Java应用程序来记录声音。我想这将是基本上是将在客户端上运行的小程序。但是我没有怎么做任何的想法...任何想法?另外,我想播放录制的声音。

我听说过的Java语音API的。任何想法,如果它可以帮助?

java audio audio-recording applet
1个回答
7
投票

我迟到了,但这里有上捕获音频的官方文档:http://docs.oracle.com/javase/tutorial/sound/capturing.html

(直接从上面这里的链接复制是一些示例代码做到这一点:)

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);
}
© www.soinside.com 2019 - 2024. All rights reserved.