簡體   English   中英

如何閱讀MP3

[英]How to Read MP3

通過這篇文章,我能夠找到如何讀取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() 在數據不足時返回-1 ,因此您可以在發生這種情況時中斷循環:

while (true){
    int currentByte = din.read();
    if (currentByte == -1) break;
    // Handling code
}

嘗試以下代碼:import 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM