Android - 创建音频文件的波形(OGG)

时间:2014-11-13 16:28:05

标签: java android audio ogg

如何创建ogg文件的简单波形(如RingDroid中所示)?有什么简单的方法吗?

我找到了RingDroid(https://code.google.com/p/ringdroid/)的源代码,但它不支持ogg格式。我使用的是Android 4.0 +。

有没有简单的方法呢?

感谢您的建议。

编辑:我想创建这样的东西(整首歌的波形)

enter image description here

我希望,很清楚。如果您有任何疑问,请询问。

1 个答案:

答案 0 :(得分:0)

您可以使用Visualizer获取波形数据

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.

检查此视图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,了解如何从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);