如何在记录时检索数据并将其以图形(可视化工具)形式显示在屏幕上?

发布于 2024-12-03 03:17:39 字数 3472 浏览 3 评论 0原文

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

}

上面是我对 MediaRecord 的设置。我在录制时使用“datacaptureListener”来获取数据。

我设置了 Visualizer(0),它被定义为从外部通道获取字节流。我可以完美地将麦克风的声音录制到 .3pg 音频文件中,但我希望从录制中获得二进制或十进制数据。

我创建了另一个文件 VisualizerView.java 并使用类画布根据捕获的数据绘制图表。

现在的问题是系统只能输出数据的地址(使用函数“updateVisualizer”,

我不知道该地址是否包含我需要的数据),程序将转到函数“OnDraw”。有熟悉 Visualizer 的人可以帮助我吗?

谢谢!

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 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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文