如何阅读 MP3
How to Read MP3
使用 this post 我能够找到如何读取 mp3 文件,但我仍然对如何实际使用它感到困惑。
File file = new File(filename);
AudioInputStream in= AudioSystem.getAudioInputStream(file);
AudioInputStream din = null;
AudioFormat baseFormat = in.getFormat();
AudioFormat decodedFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
baseFormat.getSampleRate(),
16,
baseFormat.getChannels(),
baseFormat.getChannels() * 2,
baseFormat.getSampleRate(),
false);
din = AudioSystem.getAudioInputStream(decodedFormat, in);
这些声明之后,你如何使用din
?我知道循环看起来像这样:
while (/*What do I put here??*/){
int currentByte = din.read();
}
换句话说,我只是问如何从 mp3 文件中读取整个字节数组,并在循环中一次检查一个。
AudioInputStream.read() returns -1 when it's out of data,这样你就可以在这种情况发生时打破你的循环:
while (true){
int currentByte = din.read();
if (currentByte == -1) break;
// Handling code
}
试试这个代码:
导入 java.io.DataInputStream;
导入 java.io.FileInputStream;
public class Main {
public static void main(String[] args) throws Exception {
FileInputStream fin = new FileInputStream("C:/Array.txt");
DataInputStream din = new DataInputStream(fin);
byte b[] = new byte[10];
din.read(b);
din.close();
}
提供者:http://www.java2s.com/Tutorial/Java/0180__File/ReadbytearrayfromfileusingDataInputStream.htm
使用 this post 我能够找到如何读取 mp3 文件,但我仍然对如何实际使用它感到困惑。
File file = new File(filename);
AudioInputStream in= AudioSystem.getAudioInputStream(file);
AudioInputStream din = null;
AudioFormat baseFormat = in.getFormat();
AudioFormat decodedFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
baseFormat.getSampleRate(),
16,
baseFormat.getChannels(),
baseFormat.getChannels() * 2,
baseFormat.getSampleRate(),
false);
din = AudioSystem.getAudioInputStream(decodedFormat, in);
这些声明之后,你如何使用din
?我知道循环看起来像这样:
while (/*What do I put here??*/){
int currentByte = din.read();
}
换句话说,我只是问如何从 mp3 文件中读取整个字节数组,并在循环中一次检查一个。
AudioInputStream.read() returns -1 when it's out of data,这样你就可以在这种情况发生时打破你的循环:
while (true){
int currentByte = din.read();
if (currentByte == -1) break;
// Handling code
}
试试这个代码: 导入 java.io.DataInputStream; 导入 java.io.FileInputStream;
public class Main {
public static void main(String[] args) throws Exception {
FileInputStream fin = new FileInputStream("C:/Array.txt");
DataInputStream din = new DataInputStream(fin);
byte b[] = new byte[10];
din.read(b);
din.close();
}
提供者:http://www.java2s.com/Tutorial/Java/0180__File/ReadbytearrayfromfileusingDataInputStream.htm