如何在Android中降低录制的音频可视化速度?

时间:2014-10-22 12:24:42

标签: java android audio

如何在Android中降低录制的音频可视化速度?

我能够从麦克风录制语音并通过可视化播放录制的音频, 但是可视化的速度要快,我怎样才能降低可视化的速度。 下面的代码用于在android ..中绘制可视化。

public class VisualizerView1 extends View {

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

    public VisualizerView1(Context context) {
        super(context);
        init1();
    }

    private void init1() {
        mBytes = null;
        mForePaint.setStrokeWidth(2f);
        mForePaint.setAntiAlias(true);
        mForePaint.setColor(Color.rgb(0, 255, 128));
    }

    public void updateVisualizer(byte[] bytes) {
        mBytes = bytes;
        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());

        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);
    }

}

1 个答案:

答案 0 :(得分:0)

我已尝试过一些可视化代码,我只是跳过&#34;跳过&#34;每隔一帧。也许这就是你当时正在寻找的东西。

我添加了一个布尔值,然后测试是否为真,如果不是

则只是跳过代码
private boolean frameSkip = false;


@Override
protected void onDraw(Canvas canvas) 
{
    if(frameSkip)
    {
        frameSkip = false;
    }
    else
    {
        frameSkip = true;
        ...Your code here
    }
    canvas.drawLines(mPoints, mForePaint);
}