gpt4 book ai didi

java - 为什么 AudioInputStream 读取永远不会返回 -1?

转载 作者:行者123 更新时间:2023-12-01 11:45:23 25 4
gpt4 key购买 nike

我正在读取 AudioInputStream 并使用它向服务器发出分块 POST 请求,以便传输音频。我不明白为什么流上的 read() 方法总是返回 0,在调用 stop()close() 之后也是如此目标数据行。我预计在某个时候会有 -1,因为流已关闭并且没有更多数据(EOF)。这导致我在进行 POST 的 Apache HTTP Client 调用中出现问题,因为在某些时候它期望 -1 终止写入输出流,并且以这种方式永远不会终止写入循环。

这是片段:

public class MicrophoneTest {

// record duration, in milliseconds
static final long RECORD_TIME = 5000;

private static AudioFormat audioFormat = new AudioFormat(48000, 16, 1, true, false);

public static void main(String[] args) throws URISyntaxException, IOException {
final Microphone recorder = new Microphone(audioFormat);

recorder.open();

// creates a new thread that waits for a specified
// of time before stopping
Thread stopper = new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(RECORD_TIME);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
recorder.close();
}
});

stopper.start();

// start recording
AudioInputStream inputStream = recorder.start();

ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

byte[] buffer = new byte[1000];
int read = -1;
while ((read = inputStream.read(buffer)) != -1) {
System.out.println(read);
outputStream.write(buffer);
}

// Never gets here!
}
}



public class Microphone {

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

// format of audio file
private static final AudioFileFormat.Type fileType = AudioFileFormat.Type.WAVE;

// the line from which audio data is captured
private TargetDataLine line;

private AudioFormat audioFormat;

public Microphone(AudioFormat audioFormat) {
this.audioFormat = audioFormat;
}

/**
* Prepare the line for recording
*
* @return
*/
public boolean open() {
try {
Info info = AudioSystem.getMixerInfo()[4];
line = (TargetDataLine) AudioSystem.getTargetDataLine(audioFormat, info);
line.open(audioFormat);

} catch (LineUnavailableException ex) {
LOGGER.error(ex.toString(), ex);
return false;
}
return true;
}

/**
* Captures the sound and return the stream
*/
public AudioInputStream start() {

if (line != null) {

line.start(); // start capturing

LOGGER.info("Start recording...");

return new AudioInputStream(line);

// AudioSystem.write(ais, fileType, outputStream);

} else {
throw new IllegalStateException("Line has not created. Cannot start recording");
}
}

/**
* Stops the recording process
*/
public void stop() {
line.stop();
LOGGER.info("Stop recording...");
}

/**
* Closes the target data line to finish capturing and recording *
*/
public void close() {
line.stop();
line.close();
LOGGER.info("Data line closed");
}
}

最佳答案

麦克风线永远不会到达文件末尾。它要么打开,要么关闭。它不是从磁盘或内存位置读取文件。

我认为您需要将 while 循环更改为如下所示:

while(isRecording)
{
read = inputStream.read(buffer);
outputStream.write(buffer, 0, read);
}

并让您的 stop() 方法包含

isRecording = false;

你的 start() 方法包括

isRecording = true;

等等。诸如此类的事情。

关于java - 为什么 AudioInputStream 读取永远不会返回 -1?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29183056/

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