Android - create waveform of the audio file (OGG)

3.8k views Asked by At

How can I create a simple waveform of the ogg file (like in RingDroid)? Is there any simple way?

I found a source code of RingDroid (https://code.google.com/p/ringdroid/) but it´s not support ogg format. I´m using Android 4.0+.

Is there some easy way to do it?

Thanks for advice.

EDIT: I want create something like this (waveform for the whole song)

enter image description here

I hope, it´s clear. If you have some questions, please ask.

1

There are 1 answers

2
Mostafa Gazar On

You can use Visualizer to get the waveform data

MediaPlayer mediaPlayer = new MediaPlayer();
.
.
.
int audioSessionID = mediaPlayer.getAudioSessionId();

// Enable Equalizer if waveform was affected by device volume.
// Equalizer equalizer = new Equalizer(0, audioSessionID);
// equalizer.setEnabled(true);

// Need android.permission.RECORD_AUDIO.
Visualizer visualizer = new Visualizer(audioSessionID);
visualizer.setCaptureSize(Visualizer.getCaptureSizeRange()[1]);
visualizer.setDataCaptureListener(new Visualizer.OnDataCaptureListener() {
            public void onWaveFormDataCapture(Visualizer visualizer, byte[] bytes,
                    int samplingRate) {
                // Send bytes (waveform) to your custom UI view to show it.
            }

            public void onFftDataCapture(Visualizer visualizer, byte[] bytes, int samplingRate) {}
        }, Visualizer.getMaxCaptureRate() / 2, true, false);
visualizer.setEnabled(true);

mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            public void onCompletion(MediaPlayer mediaPlayer) {
                visualizer.setEnabled(false);
            }
        });

// TODO :: Do not forget to release visualizer onPause.

Check this view http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android-apps/4.4.4_r1/com/example/android/apis/media/AudioFxDemo.java#VisualizerView on how to draw the waveform from the data retrieved from the Visualizer

if (mPoints == null || mPoints.length < mBytes.length * 4) {
    mPoints = new float[mBytes.length * 4];
}

mRect.set(0, 0, getWidth(), getHeight());

for (int i = 0; i < mBytes.length - 1; i++) {
    mPoints[i * 4] = mRect.width() * i / (mBytes.length - 1);
    mPoints[i * 4 + 1] = mRect.height() / 2
            + ((byte) (mBytes[i] + 128)) * (mRect.height() / 2) / 128;
    mPoints[i * 4 + 2] = mRect.width() * (i + 1) / (mBytes.length - 1);
    mPoints[i * 4 + 3] = mRect.height() / 2
            + ((byte) (mBytes[i + 1] + 128)) * (mRect.height() / 2) / 128;
}

canvas.drawLines(mPoints, mForePaint);