我使用MediaRecorder记录环境声音并计算其分贝。
现在,我想绘制分贝的活动图表。
我使用AChartEngine,但不知道如何绘制:
是否将数据库保存在Arrey中?
PLZ帮帮我,
提前致谢。
那是我的代码:
TextView tvDecible;
TextView tvRecord;
MediaPlayer mPlayer;
MediaRecorder mRecorder = null;
double mEMA = 0.0;
double REFERENCE = 1.0;
Timer timer = new Timer();
GraphicalView mChart;
XYMultipleSeriesDataset mDataset = new XYMultipleSeriesDataset();
XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer();
XYSeries mCurrentSeries;
XYSeriesRenderer mCurrentRenderer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnPlay = (Button) this.findViewById(R.id.btnDecible);
tvDecible = (TextView) this.findViewById(R.id.tvDecible);
Button btnRecord = (Button) this.findViewById(R.id.btnRecord);
final Button btnStop = (Button) this.findViewById(R.id.btnStop);
tvRecord = (TextView) this.findViewById(R.id.tvRecord);
mPlayer = MediaPlayer.create(this, R.raw.sound5);
initChart();
btnPlay.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
AudioManager audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
int origionalVolume = audio
.getStreamVolume(AudioManager.STREAM_MUSIC);
audio.setStreamVolume(AudioManager.STREAM_MUSIC,
audio.getStreamMaxVolume(AudioManager.STREAM_MUSIC), 0);
mPlayer.start();
int currentVolume = audio
.getStreamVolume(AudioManager.STREAM_MUSIC);
tvDecible.setText(String.valueOf(origionalVolume + "_"
+ currentVolume));
}
});
btnRecord.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startRecording();
timer.scheduleAtFixedRate(tTask, 0, 1000);
}
});
btnStop.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
timer.cancel();
stopRecording();
}
});
}
TimerTask tTask = new TimerTask() {
@Override
public void run() {
// TODO Auto-generated method stub
int db = (int) getAmplitudeEMA();
Log.i("Decibel", String.valueOf(db));
onResume();
if (db >= 100) {
runOnUiThread(new Runnable() {
public void run() {
Uri alarm = RingtoneManager
.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone ring = RingtoneManager.getRingtone(
getApplicationContext(), alarm);
ring.play();
}
});
} else {
}
}
};
public void startRecording() {
if (mRecorder == null) {
mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mRecorder.setOutputFile(getFilename());
try {
mRecorder.prepare();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mRecorder.start();
mEMA = 0.0;
}
}
public void stopRecording() {
if (mRecorder != null) {
mRecorder.stop();
mRecorder.release();
mRecorder = null;
}
}
@SuppressLint("SdCardPath")
private String getFilename() {
File file = new File("/sdcard", "AUDIO_RECORDER_FOLDER");
if (!file.exists()) {
file.mkdirs();
}
return (file.getAbsolutePath() + "/" + System.currentTimeMillis() + ".mp3");
}
public double getAmplitude() {
if (mRecorder != null)
return (mRecorder.getMaxAmplitude());
else
return 0;
}
public double getAmplitudeEMA() {
double amp = getAmplitude();
mEMA = 20 * Math.log10(amp / REFERENCE);
return mEMA;
}
private void initChart() {
mCurrentSeries = new XYSeries("Sample Data");
mDataset.addSeries(mCurrentSeries);
mCurrentRenderer = new XYSeriesRenderer();
mRenderer.addSeriesRenderer(mCurrentRenderer);
}
protected void onResume() {
super.onResume();
LinearLayout layout = (LinearLayout) findViewById(R.id.chart);
if (mChart == null) {
initChart();
addData();
mChart = ChartFactory.getCubeLineChartView(this, mDataset,
mRenderer, 0.3f);
layout.addView(mChart);
}
else {
mChart.repaint();
}
}
public void addData() {
// TODO Auto-generated method stub
mCurrentSeries.add(x[k], y[k]);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
最佳答案
由于您的声音实际上是时间的函数(如振幅值)
特定时间),使用TimeSeries而不是XYSeries会更容易。
对于简单的事情,如下所示:
TimeSeries mCurrentSeries = new TimeSeries("Sample Data");
...
public void addData(float amplitude) {
Date currentTime = new Date();
mCurrentSeries.add(currentTime, amplitude);
mChart.repaint();
}
是否要在数据库/阵列中保存数据完全取决于您。
如果它在数据库中,只需在onCreate中读取它的所有数据并将其添加
到TimeSeries。由于时间序列是某种形式的集合,因此您拥有
“数组”已经存在。
希望我能正确回答你的问题。