How to get audio data from a MP3?

后端 未结 3 632
伪装坚强ぢ
伪装坚强ぢ 2020-12-13 00:55

I\'m working on an application that has to process audio files. When using mp3 files I\'m not sure how to handle data (the data I\'m interested in are the the audio bytes, t

相关标签:
3条回答
  • 2020-12-13 01:33

    As mentioned in the other answers, you need a decoder to decode MP3 into regular audio samples.

    One popular option would be JavaLayer (LGPL).

    0 讨论(0)
  • 2020-12-13 01:41

    The data that you want are the actual samples, while MP3 represents the data differently. So, like what everyone else has said - you need a library to decode the MP3 data into actual samples for your purpose.

    0 讨论(0)
  • 2020-12-13 01:48

    From the documentation for MP3SPI:

    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);
    

    You then just read data from din - it will be the "raw" data as per decodedFormat. (See the docs for AudioFormat for more information.)

    (Note that this sample code doesn't close the stream or anything like that - use appropriate try/finally blocks as normal.)

    0 讨论(0)
提交回复
热议问题