从 Jar 中提取时声音失真
Sound distortion when extracting from Jar
我的 jar 文件目录中有声音。
我需要使用这些声音,我正在尝试使用这种方法提取它们:
String charset = "ISO-8859-1";
public void extractSounds(String pathIn, String pathOut) throws IOException {
BufferedReader r = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream(pathIn), charset));
String line = r.readLine();
String result = null;
FileOutputStream fos = new FileOutputStream(pathOut);
while(line != null) {
if(result != null) {
result += "\r" + line;
line = r.readLine();
} else {
result = line;
line = r.readLine();
}
}
fos.write(result.getBytes(charset));
}}
但是当我提取声音时,它们会失真,我不知道是什么问题,因为它基本上只是复制文件。
声音:
Original,
Extracted
如果您能帮助我找到解决方案或建议其他提取声音文件的方法,我将不胜感激。
不要假设你正在阅读文本。您不应该尝试改变数据。只需分块复制即可。
试试
InputStream in = ...;
ByteArrayOutputStream out = new ByteArrayOutputStream();
final int BUF_SIZE = 1 << 8;
byte[] buffer = new byte[BUF_SIZE];
int bytesRead = -1;
while((bytesRead = in.read(buffer)) > -1) {
out.write(buffer, 0, bytesRead);
}
我的 jar 文件目录中有声音。 我需要使用这些声音,我正在尝试使用这种方法提取它们:
String charset = "ISO-8859-1";
public void extractSounds(String pathIn, String pathOut) throws IOException {
BufferedReader r = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream(pathIn), charset));
String line = r.readLine();
String result = null;
FileOutputStream fos = new FileOutputStream(pathOut);
while(line != null) {
if(result != null) {
result += "\r" + line;
line = r.readLine();
} else {
result = line;
line = r.readLine();
}
}
fos.write(result.getBytes(charset));
}}
但是当我提取声音时,它们会失真,我不知道是什么问题,因为它基本上只是复制文件。 声音: Original, Extracted
如果您能帮助我找到解决方案或建议其他提取声音文件的方法,我将不胜感激。
不要假设你正在阅读文本。您不应该尝试改变数据。只需分块复制即可。
试试
InputStream in = ...;
ByteArrayOutputStream out = new ByteArrayOutputStream();
final int BUF_SIZE = 1 << 8;
byte[] buffer = new byte[BUF_SIZE];
int bytesRead = -1;
while((bytesRead = in.read(buffer)) > -1) {
out.write(buffer, 0, bytesRead);
}