2

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.

Pepa Zapletal
  • 2,879
  • 3
  • 39
  • 69

1 Answers1

0

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);
Mostafa Gazar
  • 2,487
  • 18
  • 23
  • Thanks for you answer. I´m using something like this now and it´s create a waveform of the current playing part of the song, but not the waveform of the whole song. Do you understand me? Do you know how to change it, to create waveform for the whole song? – Pepa Zapletal Nov 13 '14 at 21:17
  • @PepaZapletal have you found anything realted to it ? – Abhishek Aug 12 '16 at 04:21