开发者

How to retrieve data when recording and display it as graphs(Visualizer) on 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);开发者_如何学C
    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 is my settings for MediaRecord. I use the "datacaptureListener" to get data when recording.

I set Visualizer(0), it is defined to get byte flow from outside channel. I can record sound from microphone into .3pg audio file perfectly, but I hope to get binary or decimal data from recording.

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

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

I donot know if the address contains the data I need) and the program will go to function "OnDraw". Is there anyone who are familiar with Visualizer can help me?

THX!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜