gpt4 book ai didi

java - 如何在Java中捕获声音?

转载 作者:行者123 更新时间:2023-12-03 01:35:34 25 4
gpt4 key购买 nike

我正在尝试捕捉PC的声音。我已经设法捕获了通过TargetDataLine进入麦克风的声音,但是我找不到捕获来自扬声器的声音的方法。

我一直在看mixer,但没有设法捕捉声音。我想知道是否有人这样做,以及您能否给我一些从哪里开始的线索。

最佳答案

虽然,您的问题不是真的按照“规则”进行,但是下面是一个代码段:

private byte[] record() throws LineUnavailableException {
AudioFormat format = AudioUtil.getAudioFormat(audioConf);
DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);

// Checks if system supports the data line
if (!AudioSystem.isLineSupported(info)) {
LOGGER.error("Line not supported");
System.exit(0);
}

microphone = (TargetDataLine) AudioSystem.getLine(info);
microphone.open(format);
microphone.start();

LOGGER.info("Listening, tap enter to stop ...");

ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
int numBytesRead;
byte[] data = new byte[microphone.getBufferSize() / 5];

// Begin audio capture.
microphone.start();

// Here, stopped is a global boolean set by another thread.
while (!stopped) {
// Read the next chunk of data from the TargetDataLine.
numBytesRead = microphone.read(data, 0, data.length);
// Save this chunk of data.
byteArrayOutputStream.write(data, 0, numBytesRead);
}

return byteArrayOutputStream.toByteArray();
}

从这里获取更多信息:
https://www.programcreek.com/java-api-examples/?class=javax.sound.sampled.TargetDataLine&method=read

关于java - 如何在Java中捕获声音?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53583189/

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