如何从麦克风录制声音?

时间:2019-08-09 19:57:18

标签: java audio 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的字节数组。

有人能帮忙吗?

1 个答案:

答案 0 :(得分:0)

好吧,因此,在使用笔记本电脑进行了大量的反复学习之后,解决方案是安装新版本的Eclipse,这时Mac询问我是否要允许Java权限来使用麦克风。

希望这会对在那里的人有所帮助。