Android - Create Waveform Of The Audio File (ogg)
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
Solution 1:
You can use Visualizer to get the waveform data
MediaPlayermediaPlayer=newMediaPlayer();
.
.
.
intaudioSessionID= 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.Visualizervisualizer=newVisualizer(audioSessionID);
visualizer.setCaptureSize(Visualizer.getCaptureSizeRange()[1]);
visualizer.setDataCaptureListener(newVisualizer.OnDataCaptureListener() {
publicvoidonWaveFormDataCapture(Visualizer visualizer, byte[] bytes,
int samplingRate) {
// Send bytes (waveform) to your custom UI view to show it.
}
publicvoidonFftDataCapture(Visualizer visualizer, byte[] bytes, int samplingRate) {}
}, Visualizer.getMaxCaptureRate() / 2, true, false);
visualizer.setEnabled(true);
mediaPlayer.setOnCompletionListener(newMediaPlayer.OnCompletionListener() {
publicvoidonCompletion(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 = newfloat[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);
Post a Comment for "Android - Create Waveform Of The Audio File (ogg)"