社区首页 >问答首页 >为什么AudioInputStream read从不返回-1?问为什么AudioInputStream read从不返回-1?ENStack Overflow用户提问于 2015-03-21 21:04:25回答 1查看 297关注 0票数 0我正在读取一个AudioInputStream,并使用它向服务器发出一个分块的POST请求,以便流式传输音频。我不明白为什么流上的read()方法总是返回0,在TargetDataLine上调用stop()和close()之后也是如此。我希望在某个时刻有-1,因为流是关闭的,并且上面没有更多的数据(EOF)。这在进行POST的Apache HTTP客户端调用中引起了问题,因为在某个时刻,它期望-1终止写入输出流,并且以这种方式永远不会终止写入循环。
下面是代码片段:
代码语言:javascript复制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");
}
}javastreammicrophonejavasound关注问题分享EN回答 1推荐最新Stack Overflow用户发布于 2015-03-22 13:04:19
麦克风线路永远不会到达文件的末尾。它要么是打开的,要么是关闭的。它不是从磁盘或内存位置读取文件。
我认为您需要将while循环更改为如下所示:
代码语言:javascript复制while(isRecording)
{
read = inputStream.read(buffer);
outputStream.write(buffer, 0, read);
}并让您的stop()方法包含
代码语言:javascript复制isRecording = false;而start()方法包括
代码语言:javascript复制isRecording = true;诸如此类的事情。
收藏分享票数 1EN页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持原文链接:https://stackoverflow.com/questions/29183056
复制相关文章