I wrote an android program that detects if the room is noisy or quite. But whenever I rotate the phone , the text message and image won't get updated any more and the noise decibel is printed - infinity and its correct value. I think that the every time I rotate the phone a new thread being created. Because amplitudeDb amount is being printed one more time with a value of - infinity if I rotate my phone. How Can I prevent this from happening? How can I keep the initial thread running without a new thread being generated each time I rotate the phone?
here is my code
public class EnvironmentalNoise extends AppCompatActivity {
Context mContext;
private MediaRecorder mRecorder = null;
double soundLevel;
SoundMeter sm;
ImageView noiseImage;
TextView noiseTv;
double amplitudeDb;
boolean mediaRecorderExist;
boolean isTreadRunning;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_environmental_noise);
noiseImage = findViewById(R.id.noiseImage);
noiseTv = findViewById(R.id.noiseTv);
mContext = getApplicationContext();
PackageManager PM= this.getPackageManager();
sm = new SoundMeter();
final boolean microphone = PM.hasSystemFeature(PackageManager.FEATURE_MICROPHONE);
amplitudeDb =0;
mediaRecorderExist = false;
isTreadRunning =false;
if (microphone){
sm.start();
}
else{
noiseTv.setText("Sorry !!! This device is not equipped to microphone to detect environmental noise");
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
Log.d("SaveState", "onSaveInstanceState called");
//save current amplitudeDb value in bundle key - value
outState.putDouble("SAVED_STATE_COUNT_KEY", amplitudeDb);
outState.putBoolean("MEDIA_RECORDER_EXIST",mediaRecorderExist);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
Log.d("SaveState", "onRestoreInstanceState called");
//retrieve current counter value from bundle based on key
int retrievedNoiseDecibel = savedInstanceState.getInt("SAVED_STATE_COUNT_KEY");
boolean retrievedMediaRecorder = savedInstanceState.getBoolean("MEDIA_RECORDER_EXIST");
if(retrievedMediaRecorder){
retrievedMediaRecorder = false;
}
//update text view
if(retrievedNoiseDecibel>60){
noiseTv.setText("This room is noisy!!!");
noiseImage.setBackgroundResource(R.drawable.noise);
}
else{
noiseTv.setText("This room is quiet!!!");
noiseImage.setBackgroundResource(R.drawable.quiet);
}
Log.d("SaveState", "retrieved counter value:" + retrievedNoiseDecibel);
//Toast.makeText(this, "retrieved counter value:" + retrievedCounter, Toast.LENGTH_LONG).show();
amplitudeDb = retrievedNoiseDecibel; //update total number of clicks
mediaRecorderExist = retrievedMediaRecorder;
}
//start of refrencing
// found this piece of code from here : https://stackoverflow.com/questions/31305163/getmaxamplitude-always-returns-0
public class SoundMeter {
private MediaRecorder mRecorder = null;
public void start() {
if(mediaRecorderExist){
mRecorder.stop();
mRecorder.release();
mRecorder = null;
}
if (mRecorder == null) {
mRecorder = new MediaRecorder();
mediaRecorderExist =true;
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
Timer timer = new Timer();
//timer.schedule(new EnvironmentalNoise.RecorderTask(mRecorder), 0);
timer.scheduleAtFixedRate(new RecorderTask(mRecorder), 0, 500);
mRecorder.setOutputFile("/dev/null");
try {
mRecorder.prepare();
mRecorder.start();
} catch (IOException e) {
e.printStackTrace();
}
catch (RuntimeException e){
e.printStackTrace();
}
}
}
private class RecorderTask extends TimerTask {
//TextView sound = (TextView) findViewById(R.id.decibel);
private MediaRecorder recorder;
public RecorderTask(MediaRecorder recorder) {
this.recorder = recorder;
}
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
int peakAmplitude = recorder.getMaxAmplitude();
//double amplitudeDb = 20 * Math.log10((double)Math.abs(peakAmplitude));
amplitudeDb = 20 * Math.log10((double)Math.abs(peakAmplitude));
Log.i("sound","amplitudeDb" + amplitudeDb);
if (amplitudeDb>60){
noiseTv.setText("This room is noisy!!!");
noiseImage.setBackgroundResource(R.drawable.noise);
}
else{
noiseTv.setText("This room is quiet!!!");
noiseImage.setBackgroundResource(R.drawable.quiet);
}
}
});
}
}
}// end of referencing