gpt4 book ai didi

android - 绘制动态图表以显示声音的分贝数

转载 作者:行者123 更新时间:2023-11-30 03:10:30 26 4
gpt4 key购买 nike

我使用 MediaRecorder 来记录环境声音并计算它的分贝数。现在我想为分贝绘制生动的图表。我使用 AChartEngine 但我不知道如何绘制它:是否将数据库保存在数组中??LZ帮帮我,提前致谢。

这是我的代码:

    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 中从中读取所有数据并添加它时间序列。由于时间序列在某种程度上是一种集合,因此您有自己的已经是“数组”了。

希望我答对了你的问题。

关于android - 绘制动态图表以显示声音的分贝数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21071854/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com