Android Open Source - tunnel_player_workaround Simple Visualizer View






From Project

Back to project page tunnel_player_workaround.

License

The source code is released under:

This is free and unencumbered software released into the public domain. Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a co...

If you think the Android project tunnel_player_workaround listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.h6ah4i.android.example.tunnelplayerworkaround;
//from w ww  .  j  a v a  2 s .  c o m
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.media.audiofx.Visualizer;
import android.util.AttributeSet;
import android.view.View;

public class SimpleVisualizerView
        extends View
        implements Visualizer.OnDataCaptureListener {

    private Visualizer mVisualizer;
    private volatile byte[] mWaveform;
    private Paint mPaint;

    public SimpleVisualizerView(Context context) {
        super(context);
    }

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

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

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
    }

    private Paint getPaint() {
        if (mPaint == null) {
            mPaint = new Paint();
            mPaint.setColor(0xffff0000);
        }
        return mPaint;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        final byte[] waveform = mWaveform;

        if (waveform == null)
            return;
        
        // draw waveform

        final Paint p = getPaint();
        final int width = getWidth();
        final int height = getHeight();

        final float hcoeff = 2.0f;
        final float voffset = height / 2;
        final float htick = (float) width / waveform.length;

        float pv = 0;
        for (int i = 0; i < waveform.length; i++) {
            float v = hcoeff * ((byte) (waveform[i] + 128));
            canvas.drawLine(
                    htick * i, (voffset + pv),
                    htick * (i + 1), (voffset + v),
                    p);
            pv = v;
        }
    }

    public void bindAudioSession(int audioSession) {
        if (mVisualizer != null) {
            mVisualizer.setEnabled(false);
            mVisualizer.setDataCaptureListener(null, 0, false, false);
        }

        mVisualizer = new Visualizer(audioSession);
        mVisualizer.setEnabled(false);
        mVisualizer.setCaptureSize(Visualizer.getCaptureSizeRange()[1]);
        mVisualizer.setDataCaptureListener(this,
                Visualizer.getMaxCaptureRate() / 2, true, false);
    }

    public void onResume() {
        if (mVisualizer != null) {
            mVisualizer.setEnabled(true);
        }
    }

    public void onPause() {
        if (mVisualizer != null) {
            mVisualizer.setEnabled(false);
        }
    }

    @Override
    public void onFftDataCapture(
            Visualizer visualizer, byte[] fft, int samplingRate) {
    }

    @Override
    public void onWaveFormDataCapture(
            Visualizer visualizer, byte[] waveform, int samplingRate) {
        mWaveform = waveform;
        this.postInvalidate();
    }
}




Java Source Code List

com.h6ah4i.android.example.tunnelplayerworkaround.MainActivity.java
com.h6ah4i.android.example.tunnelplayerworkaround.SimpleVisualizerView.java
com.h6ah4i.android.music_player.MediaPlayerLocalService.java
com.h6ah4i.android.utils.LocalServiceBinder.java