硬件话筒控制

时间:2012-03-17 11:58:51

标签: java javasound

如何使用java代码控制麦克风开关功能?我需要控制麦克风打开的时间。 我尝试在java中使用以下代码:

final AudioFormat format = getFormat();//getformat() has the audio format
    DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
    final TargetDataLine line   = (TargetDataLine) AudioSystem.getLine(info);

    line.open(format);                         //open mic for input
line.start();
    byte[] buffer = new byte[1048576];
OutputStream out = new ByteArrayOutputStream();//output the audio to buffer
boolean running = true;

    try {
        while (running) {
        int count = line.read(buffer, 0, buffer.length);
        running=false;
            if (count > 0) {
            out.write(buffer, 0, count);               
            }
        }
            out.close();                             

    } catch (IOException e) {
    System.out.println("Error");
        System.err.println("I/O problems: " + e);
        System.exit(-1);
          }

但这主要取决于缓冲区的大小。并且 while 循环可以每次传输30秒的音频。 我需要将样本输入仅用10秒。 任何帮助?感谢:)

1 个答案:

答案 0 :(得分:1)

似乎你试图通过缓冲区的大小来控制拾音的持续时间。我很确定这不常见。通常一个人使用一小部分大小的缓冲区(以保持低延迟),并重复迭代它。要控制开放式读取或回放操作的持续时间,更常见的是更改“运行”布尔值。

因此,从循环外部,代码更新“running”布尔值,当循环注意到有停止请求时,读取循环结束。

我不知道如何获得开启麦克风的许可。我知道java声音教程谈论它。

http://docs.oracle.com/javase/tutorial/sound/capturing.html

在他们的例子中,他们使用布尔“停止”来控制何时结束录制循环。

相关问题