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的人可以帮助我?
THX!