Android Visualizer implementation crash

好久不见. 提交于 2019-11-27 14:14:44

I have same problem, so try to add permission in AndroidManifest.xml file

Also if you wan`t to see that audi wave, you hould show it in canvas or something ...

Working example :

//android.permission.MODIFY_AUDIO_SETTINGS for audio settings and also
//android.permission.INTERNET for internet streaming

package com.janilemy;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.audiofx.Visualizer;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import java.io.IOException;

public class AudioFxDemoActivity extends Activity {

    //Here is your URL defined
String url = "http://vprbbc.streamguys.net/vprbbc24.mp3";

    //Constants for vizualizator - HEIGHT 50dip
private static final float VISUALIZER_HEIGHT_DIP = 50f;

    //Your MediaPlayer
MediaPlayer mp;

//Vizualization
private Visualizer mVisualizer;

    private LinearLayout mLinearLayout;
    private VisualizerView mVisualizerView;
    private TextView mStatusTextView;


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    //Info textView
    mStatusTextView = new TextView(this);

    //Create new LinearLayout ( because main.xml is empty )
    mLinearLayout = new LinearLayout(this);
    mLinearLayout.setOrientation(LinearLayout.VERTICAL);
    mLinearLayout.addView(mStatusTextView);

    //set content view to new Layout that we create
    setContentView(mLinearLayout);

    //start media player - like normal
    mp = new MediaPlayer();
    mp.setAudioStreamType(AudioManager.STREAM_MUSIC);

    try {
        mp.setDataSource(url); // set data source our URL defined
    } catch (IllegalArgumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }       

    try {   //tell your player to go to prepare state
        mp.prepare(); 
    } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
            //Start your stream / player
    mp.start();

    //setup your Vizualizer - call method
    setupVisualizerFxAndUI();        

            //enable vizualizer
            mVisualizer.setEnabled(true);

            //Info text
    mStatusTextView.setText("Playing audio...");
}

    //Our method that sets Vizualizer
private void setupVisualizerFxAndUI() {
    // Create a VisualizerView (defined below), which will render the simplified audio
    // wave form to a Canvas.

    //You need to have something where to show Audio WAVE - in this case Canvas
    mVisualizerView = new VisualizerView(this);
    mVisualizerView.setLayoutParams(new ViewGroup.LayoutParams(
            ViewGroup.LayoutParams.FILL_PARENT,
            (int)(VISUALIZER_HEIGHT_DIP * getResources().getDisplayMetrics().density)));
    mLinearLayout.addView(mVisualizerView);

    // Create the Visualizer object and attach it to our media player.
    //YOU NEED android.permission.RECORD_AUDIO for that in AndroidManifest.xml
    mVisualizer = new Visualizer(mp.getAudioSessionId());
    mVisualizer.setCaptureSize(Visualizer.getCaptureSizeRange()[1]);
    mVisualizer.setDataCaptureListener(new Visualizer.OnDataCaptureListener() {
        public void onWaveFormDataCapture(Visualizer visualizer, byte[] bytes,
                int samplingRate) {
            mVisualizerView.updateVisualizer(bytes);
        }

        public void onFftDataCapture(Visualizer visualizer, byte[] bytes, int samplingRate) {}
    }, Visualizer.getMaxCaptureRate() / 2, true, false); 
}

@Override
protected void onPause() {
    super.onPause();

    if (isFinishing() && mp != null) {
        mVisualizer.release();
        mEqualizer.release();
        mp.release();
        mp = null;
    }
}

/**
 * A simple class that draws waveform data received from a
 * {@link Visualizer.OnDataCaptureListener#onWaveFormDataCapture }
 */
class VisualizerView extends View {
    private byte[] mBytes;
    private float[] mPoints;
    private Rect mRect = new Rect();

    private Paint mForePaint = new Paint();

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

    private void init() {
        mBytes = null;

        mForePaint.setStrokeWidth(1f);
        mForePaint.setAntiAlias(true);
        mForePaint.setColor(Color.rgb(0, 128, 255));
    }

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

}

This is a full working example of android media player audio wave visualization... I hope it will help you with your solution ...

Jani L

Xar E Ahmer

There are some important things for using Visualizer.

  1. minimum api level must be equal to or greater than 9.
  2. Visualizer engine, error: -1 means RECORD_AUDIO permission is missing.
  3. Visualizer engine, error: -4 means Operation failed due to bad parameter. value. something bad in the configuration of the Visualizer, you don't properly setup Visualizer.

And also see my answer here.

Note

Parameters (audioSession) system wide unique audio session identifier. If audioSession is not 0, the visualizer will be attached to the MediaPlayer or AudioTrack in the same audio session. Otherwise, the Visualizer will apply to the output mix.

You are missing Internet permission

<uses-permission android:name="android.permission.INTERNET" />
Pranav Jadav

U cant make any changes to object of visulizer after enabling to it. So just interchange to lines as shown in the belove code.

public class MediaPlayerActivity extends Activity {

    Visualizer visual;
    int formattedVizData[];
    byte rawWaveForm[];
    int cont = 0xFF;

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // Start media player & visualizer.
        MediaPlayer mediaPlayer = new MediaPlayer();
        mediaPlayer = MediaPlayer.create(this, Uri.parse("http://vprbbc.streamguys.net:80/vprbbc24.mp3"));
        mediaPlayer.start();

        // Create the Visualizer object and attach it to our media player.
        try {

            // this line is not actually causing Exception ,It is because u are
            // enabling the visulizer to capture data & and after that setting
            // the capture size of buffer. U can't make any changes after
            // enabling it. I tried this same code and it's working fine for me
            visual = new Visualizer(mediaPlayer.getAudioSessionId());

            visual.setCaptureSize(Visualizer.getCaptureSizeRange()[1]);
            visual.setEnabled(true);
        } 
        catch (Exception ex) {
            Log.e("Visual Ex", ex.getMessage());
        }

    }

}

Refere to this link for best example https://github.com/felixpalmer/android-visualizer

after doing all if visualization doesnt start then try visualizationview.startRendering()

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!