如何录制麦克风的声音?

How to record sound from the microphone?

我正在尝试了解如何使用我的 MacBookPro 从麦克风录制声音。我不确定这是代码问题还是 MacOS 中的隐藏权限问题。

我已经学习了大部分教程并尝试了各种不同的解决方案,以至于我创建了下面的测试用例。

public class AudioLinesTester {

    private static final Logger LOGGER = LoggerFactory.getLogger(AudioLinesTester.class);

    public static void main(String[] args) {

        LOGGER.debug("Starting Line tester");

        AudioFormat format = new AudioFormat(8000.0f, 8, 1, true, true);

        Mixer.Info[] infos = AudioSystem.getMixerInfo();

        Map<Line.Info, byte[]> sounds = new HashMap<Line.Info, byte[]>();

        Stream.of(infos).forEach(mixerInfo -> {
            Mixer mixer = AudioSystem.getMixer(mixerInfo);
            Line.Info[] lineInfos = mixer.getTargetLineInfo();
            if (lineInfos.length > 0 && lineInfos[0].getLineClass().equals(TargetDataLine.class)) {
                // The name of the AudioDevice
                LOGGER.debug("Line Name: {}", mixerInfo.getName());
                // The type of audio device
                LOGGER.debug("Line Description: {}", mixerInfo.getDescription());

                for (Line.Info lineInfo : lineInfos) {
                    LOGGER.debug("\t---{}", lineInfo);
                    Line line;
                    try {
                        line = mixer.getLine(lineInfo);
                        TargetDataLine microphone = AudioSystem.getTargetDataLine(format, mixerInfo);
                        microphone.open(format, 100352);

                        LOGGER.debug("Listening on {}", microphone);

                        ByteArrayOutputStream out = new ByteArrayOutputStream();
                        int numBytesRead;
                        int CHUNK_SIZE = 1024;
                        byte[] data = new byte[microphone.getBufferSize() / 5];
                        microphone.start();

                        int bytesRead = 0;

                        try {
                            // Just so I can test if recording my mic works...
                            while (bytesRead < 100000) { 
                                numBytesRead = microphone.read(data, 0, CHUNK_SIZE);
                                bytesRead = bytesRead + numBytesRead;
//                              LOGGER.debug("Bytes read from the microphone : {} ", bytesRead);
                                out.write(data, 0, numBytesRead);
                            }
                        } catch (Exception e) {
                            LOGGER.error("Error trying to read from the microphone", e);
                        }
                        byte[] read = out.toByteArray();
                        LOGGER.debug("Have read {} bytes: {}", read.length, read);
                        sounds.put(lineInfo, read);

//                      microphone.drain();
                        microphone.stop();
                        microphone.close();

                    } catch (LineUnavailableException e) {
                        LOGGER.error("Something went wrong {}", e);
                        return;
                    }
                    LOGGER.debug("\t-----{}", line);
                }
            }
        });

        sounds.forEach((k,v) -> {
            LOGGER.debug("Trying to play the data capture on {}", k);
            DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
            LOGGER.debug("Got a new output line: {}", info);
            SourceDataLine line;
            try {
                line = (SourceDataLine) AudioSystem.getLine(info);

                line.open(format, v.length);
                line.start();
                long now = System.currentTimeMillis();
                LOGGER.debug("buffer size: {}", line.available());
                int written = line.write(v, 0, v.length);
                line.drain();
                line.close();
                LOGGER.debug("{} bytes written.", written);
                long total = System.currentTimeMillis() - now;
                LOGGER.debug("{}ms.", total);
            } catch (LineUnavailableException e) {
                LOGGER.error("Unable to play the sound", e);
            }
        });

        LOGGER.debug("Ending Lines tester");
    }

}

我希望从某些东西中得到一些输出,但我只是得到填充了 0 的字节数组。

有人能帮忙吗?

好吧,在与笔记本电脑来来回回打了很多次之后...解决方案是安装新版本的 Eclipse,此时 Mac 问我是否想要允许 Java 访问麦克风的权限。

希望这对外面的人有所帮助。