这是对我以前的问题的跟进。
当我使用大小为 1024 * 32 的示例中的字节数组时,应该是波形文件的结果文件太晚了。如果我使用较小的大小,比如只有 32 个字节,甚至像一个字节一样
fstr.write(this.stream.read());
它完美地工作。
以下代码:
import java.io.*;
class ErrorThread extends Thread {
InputStream stream = null;
public ErrorThread(InputStream stream) {
this.stream = stream;
}
public void run() {
try {
byte[] buf = new byte[32 * 1024];
int nRead = 0;
while ((nRead = this.stream.read()) != -1) {
}
this.stream.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
class InputThread extends Thread {
InputStream stream = null;
public InputThread(InputStream stream) {
this.stream = stream;
}
public void run() {
try {
FileOutputStream fstr = new FileOutputStream("test.wav");
int nRead = 0;
byte[] buf = new byte[1024 * 32];
while ((nRead = this.stream.read(buf, 0, buf.length)) != -1) {
fstr.write(buf, 0 , buf.length);
}
this.stream.close();
fstr.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
public class Test {
public static void main(String[] args) {
try {
Process p = new ProcessBuilder("lame", "--decode", "test.mp3", "-").start();
ErrorThread et = new ErrorThread(p.getErrorStream());
InputThread it = new InputThread(p.getInputStream());
et.start();
it.start();
p.waitFor();
}
catch (Exception e) {
e.printStackTrace();
}
}
}