问题描述:

I use MediaRecorder to record the environment sound and calculate the decibel of that.

Now I want to draw alive chart for decibel.

I use AChartEngine but I don't know how to draw it :

save db in arrey or not??

PLZ help me,

Thanks in Advance.

that's my code:

 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;

}

网友答案:

Since sound in your case is actualy a function of time (as in amplitude value of particular time), it would be easier to use TimeSeries, rather then XYSeries.

For simplistic matters something like this:

    TimeSeries mCurrentSeries = new TimeSeries("Sample Data");
    ...
    public void addData(float amplitude) {
        Date currentTime = new Date();
        mCurrentSeries.add(currentTime, amplitude);
        mChart.repaint();
    }

If you want or not to save data in database/array is totaly up to you. If it is in the database just read all the data from it in onCreate and add it to TimeSeries. Since time series is in a way collection of sort, you have your "array" already.

Hope i got your question right.

网友答案:

I draw my chart by this code and it work nice:

private LinearLayout thisLayout;
private FragmentActivity thisActivity;

List<Integer> copydb = new ArrayList<Integer>();
CalculateDecibel decibel = new CalculateDecibel();

GraphicalView mChart;
XYMultipleSeriesDataset mDataset = new XYMultipleSeriesDataset();
XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer();
XYSeries mCurrentSeries;
XYSeriesRenderer mCurrentRenderer;

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    thisActivity = super.getActivity();
    thisLayout = (LinearLayout) inflater.inflate(
            R.layout.activity_db_chart, container, false);

    LinearLayout layout = (LinearLayout) thisLayout
            .findViewById(R.id.chart);
    initChart();

    mChart = ChartFactory.getCubeLineChartView(thisActivity, mDataset,
            mRenderer, 0.3f);

    layout.addView(mChart);


    return thisLayout;
}

private void initChart() {
    mCurrentSeries = new XYSeries("Sample Data");
    mDataset.addSeries(mCurrentSeries);
    mCurrentRenderer = new XYSeriesRenderer();
    mRenderer.addSeriesRenderer(mCurrentRenderer);
    mRenderer.setMarginsColor(Color.parseColor("#c05100"));
    mRenderer.setAxesColor(Color.parseColor("#AAAAAA"));
    mRenderer.setGridColor(Color.parseColor("#EEEEEE"));
    mRenderer.setMargins(new int[] { 0, 0, 0, 0 });
    mRenderer.setShowGrid(true);
    mCurrentRenderer.setColor(Color.WHITE);
    mCurrentRenderer.setPointStyle(PointStyle.SQUARE);
    mCurrentRenderer.setDisplayChartValues(true);
    mCurrentRenderer.setLineWidth(3);
    mCurrentRenderer.setFillPoints(true);

}

public void onResume() {
    super.onResume();
    // RefreshChart();
}

public void RefreshChart() {


    copydb.add(((MainActivity) getActivity()).db);



    if (((MainActivity) getActivity()).db < -10) {

        mCurrentSeries.add(mCurrentSeries.getItemCount() + 1, -10);

    } else {
        mCurrentSeries.add(mCurrentSeries.getItemCount() + 1,
                ((MainActivity) getActivity()).db);



    }


    mChart.repaint();
}


public void addList(int db) {
    copydb.add(db);
}

I wrote this code in a fragment . and call RefreshChart() in a timer that i get Db o sound in my main activity .

相关阅读:
Top