我正在尝试编写一个应用程序来在Android上录制声音并通过TCP将其发送到PC上运行的服务器。我的主要问题是缓冲接收的字节并将它们全部收集在一个文件中。
我可以通过调试我的代码看到frameLength通过读取旧内容并附加新内容而当前增长,但是Disk上的文件大小保持不变,而MediaPlayer只播放最后记录的部分。
private static final int SAMPLE_RATE = 44100;
private File waveFile;
private void toWaveFile(final byte[] soundBytes) throws IOException, UnsupportedAudioFileException {
final InputStream inputStream = new ByteArrayInputStream(soundBytes);
final AudioFormat format = new AudioFormat(SAMPLE_RATE, 16, 1, true, false);
final AudioInputStream newAudioInputStream = new AudioInputStream(inputStream, format, soundBytes.length);
if (waveFile == null) {
waveFile = new File(Config.getInstance().readSetting("tempFolder.path") + "/test_" + System.currentTimeMillis() + ".wav");
AudioSystem.write(newAudioInputStream, AudioFileFormat.Type.WAVE, waveFile);
} else {
final AudioInputStream currentContentInputStream = AudioSystem.getAudioInputStream(waveFile);
final AudioInputStream appendedFilesAudioInputStream = new AudioInputStream(
new SequenceInputStream(currentContentInputStream, newAudioInputStream),
currentContentInputStream.getFormat(),
currentContentInputStream.getFrameLength() + newAudioInputStream.getFrameLength());
AudioSystem.write(appendedFilesAudioInputStream, AudioFileFormat.Type.WAVE, waveFile);
}
}
欢迎任何帮助