Android Mediaplayer播放时设置频谱

仿网易云音乐播放时跳跃的频谱状态,此案例是根据声音的大小动态的设置跳动频率和高度,而网易云音乐是固定的。

//创建一个展示音波柱的自定义View
VisualizerView mVisualizerView = new VisualizerView();

给MediaPlay设置Visualizer监听声音频率

   private void setupVisualizer() {


        if (mVisualizer == null) {

mVisualizer = new Visualizer(mediaPlayer.getAudioSessionId());

            //设置需要转换的音乐内容长度,专业的说这就是采样,该采样值一般为2的指数倍,如64,128,256,512,1024。这里我设置了128,原因是长度越长,FFT算法运行时间更长。
            mVisualizer.setCaptureSize(Visualizer.getCaptureSizeRange()[0]);

            //rate, 表示采样的周期,即隔多久采样一次,联系前文就是隔多久采样128个数据,本文设置为512mHz更新一次(Visualizer.getMaxCaptureRate()/2)
            //iswave,是波形信号
            //isfft,是FFT信号,表示是获取波形信号还是频域信号
            mVisualizer.setDataCaptureListener(new Visualizer.OnDataCaptureListener() {


                @Overridepublic void onWaveFormDataCapture(Visualizer visualizer, byte[] waveform,
int samplingRate) {
}


                @Overridepublic void onFftDataCapture(Visualizer visualizer, byte[] fft,
int samplingRate) {

                    //将频谱设置到自定义View上显示
                    mVisualizerView.updateVisualizer(fft);
                },Visualizer.getMaxCaptureRate()/2,false,true);

                mVisualizer.setEnabled(true);

            }

        }

    }

展示音波的自定义View,我的需求是像网易云音乐类似的三个跳跃的柱形,可根据需求自行更改

package com.xui.music.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;


public class VisualizerView extends View {

    private byte[] mBytes;
    private float[] mPoints;
    private Rect mRect = new Rect();

    private Paint mForePaint = new Paint();
    private int mSpectrumNum;

    public VisualizerView(Context context) {
        super(context);
        init();
    }

    public VisualizerView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public VisualizerView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    private void init() {
        mBytes = null;

        mForePaint.setStrokeWidth(4.5f);
        mForePaint.setAntiAlias(true);
        mForePaint.setColor(Color.WHITE);
    }

    public void updateVisualizer(byte[] fft) {
        mSpectrumNum = fft.length / 2;
        byte[] model = new byte[fft.length / 2];
        for (int i = 0, j = 0; j < mSpectrumNum; j++) {
            model[j] = (byte) Math.hypot(fft[i], fft[i + 1]);
            i += 2;
        }
        mBytes = model;
        invalidate();
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (mBytes == null) {
            return;
        }

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

        mRect.set(0, 0, getWidth(), getHeight());
        //绘制柱形频谱
        final int baseX = mRect.width() / mSpectrumNum;
        final int height = mRect.height();

        for (int i = 0; i < mSpectrumNum; i++) {
            if (mBytes[i] < 0) {
                mBytes[i] = 127;
            }
            final int xi = baseX * i + baseX / 2;

            mPoints[i * 4] = xi;
            mPoints[i * 4 + 1] = height;

            mPoints[i * 4 + 2] = xi;
            mPoints[i * 4 + 3] = height - mBytes[i];
        }
        canvas.drawLines(mPoints, mForePaint);
    }
}

你可能感兴趣的:(Android)