How to extract data during recording and display them in the form of graphs (visualizer) on the screen?

Record.java

public void onClick(View v) { mRecorder = new MediaRecorder(); mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); mRecorder.setOutputFile(mFilename); mVisualizer = new Visualizer(0); mVisualizer.setCaptureSize(Visualizer.getCaptureSizeRange()[1]); mVisualizer.setDataCaptureListener(datacaptureListener,Visualizer.getMaxCaptureRate() /2,false,true); mVisualizer.setEnabled(true); mRecorder.setOnErrorListener(errorListenerForRecorder); try { mRecorder.prepare(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } try { System.out.println("****"); mRecorder.start(); } catch (Exception e) { Toast.makeText(getApplicationContext(), "Error :: " + e.getMessage(), Toast.LENGTH_LONG).show(); } } } OnDataCaptureListener datacaptureListener = new OnDataCaptureListener() { @Override public void onWaveFormDataCapture(Visualizer visualizer, byte[] bytes,int samplingRate) { System.out.println("1--->"); mVisualizerView.updateVisualizer(bytes); } public void onFftDataCapture(Visualizer visualizer, byte[] bytes, int samplingRate { System.out.println("2--->"); mVisualizerView.updateVisualizer(bytes); } }; 

Visualizerview.java

  public 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) { System.out.println("3--->"); mBytes = bytes; System.out.println(mBytes); 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); } } 

Above my settings for MediaRecord. I use the "datacaptureListener" to get data while writing.

I install Visualizer (0), it is defined to receive a byte stream from an external channel. I can record microphone sound into a .3pg audio file perfectly, but I hope to get binary or decimal data from the recording.

I created another VisualizerView.java file and used the class canvas to draw graphs according to the captured data.

The problem right now is that the system can only output the data address (using the "updateVisualizer" function,

I don’t know if the address contains the data I need), and the program will go to the "OnDraw" function. Can anyone who is familiar with Visualizer can help me?

thanks!

+4
source share

All Articles