gpt4 book ai didi

java - 如何从麦克风录制声音?

转载 作者:行者123 更新时间:2023-12-02 01:49:19 24 4
gpt4 key购买 nike

我正在尝试弄清楚如何使用 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 权限访问麦克风。

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

关于java - 如何从麦克风录制声音?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57436405/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com