Get pcm data from mp3 file in android

My question is simple. How to get PCM data from mp3 file?

Now I only found a way to get the PCM bytes from the song, downloaded and played the mp3 MediaPlayer

and tried to listen to the change of waves on Visualizer.OnDataCaptureListener

. This interface method passes a parameter byte[] bytes

. I tried to store all these bytes into one byte array and then passed that array to a method AudioTrack.write

and reproduced it with the method

. Something like that:

    mVisualizer.setDataCaptureListener(new Visualizer.OnDataCaptureListener() {
                public void onWaveFormDataCapture(Visualizer visualizer, byte[] bytes,
                        int samplingRate) {
                       // allBytes += bytes
        public void onFftDataCapture(Visualizer visualizer, byte[] bytes, int samplingRate) {
, Visualizer.getMaxCaptureRate() / 2, true, false);


and then

AudioTrack tr = new AudioTrack(AudioManager.STREAM_MUSIC, 22050, 
                              allBytes.length, AudioTrack.MODE_STREAM);;
tr.write(barr, 0, allBytes.length);


Unfortunately, I didn't expect results. The sound is playing, but there is a lot of noise in the sound, the quality is so poor that you cannot determine what is singing there.

But it was some kind of "hacking method" to get PCM data. Can you tell if there is a normal method for getting PCM data from my mp3?


source to share

1 answer

I am using the native mpg123 library to decode my MP3. It was decoded into PCM data in shorts, not bytes. I am sure you can do the conversions. The decoder is very fast as it uses the NDK.


I'm not sure how big your files are, but I would pass the bytes to AudioTrack instead of putting the entire song in one big array.



All Articles