【问题标题】:Record voice with Java用 Java 录制声音
【发布时间】:2011-06-17 02:31:10
【问题描述】:

我想使用 Java 应用程序录制语音;我想这基本上是一个在客户端运行的小程序。但我不知道该怎么做......有什么想法吗?另外,我想播放录制的声音。

我听说过 Java Speech API。知道它是否有帮助?

【问题讨论】:

标签: java audio audio-recording applet


【解决方案1】:

我迟到了,但这里是关于捕获音频的官方文档: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);
}

【讨论】:

  • 在您的示例中,我的停止行有错误,您能帮帮我吗
  • 我确实在 4 多年前写过……我确实从那个 url 复制了它,但我假设你有编译错误? stopped 变量本质上是一个变量的占位符,您应该在其他地方设置/创建/维护以了解您是否应该停止录制。所以这对你的情况非常特殊。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-10
  • 1970-01-01
  • 2016-02-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多