import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.UnsupportedAudioFileException;
public class Audio_to_bytes {
public static void main(String args[]) throws IOException
{
File WAV_FILE = new File("/home/cybersecurity/Desktop/scream.wav");
ByteArrayOutputStream out = new ByteArrayOutputStream();
AudioInputStream in = null;
try {
in = AudioSystem.getAudioInputStream(WAV_FILE);
} catch (UnsupportedAudioFileException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
int read,i;
byte[] buff = new byte[1024];
while ((read = in.read(buff)) > 0)
{
out.write(buff, 0, read);
}
out.flush();
byte[] audioBytes = out.toByteArray();
for(i=0;i<audioBytes.length;i++)
System.out.println(audioBytes[i]);
}
}
上面的代码是将 .wav 文件转换为字节数组。当我执行程序时,我得到很多值为 -128 或 -127,我想知道这些值是精确值还是成为边界值如果它超出了字节的范围(-128 到 128),如果是这样,我可以将音频文件转换为整数数组,因为它的范围更广。