I am making an app on Android studio, which records sound using the mic and simultaneously converts it to decibel values on another thread. I need to stop the process when the stopRecord function is called, and hence I want to stop the thread in the stopRecord function. I am using a static class variable for this, whose value I set to "1" when stopRecord is called. By default, the value of the variable is "0", in which case the thread must continue to run in a loop. However, the variable's value always sets to "1" unexpectedly even before the Run method of the runnable is called, which is evident if you run the file and see the Log.i . As a result, the loop in the run() of the runnable is not even entered, and the runnable runs only once. I seriously don't know why the value is changing, and any help would be greatly appreciated.
package com.example.rohanbs.soundtest;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import android.view.View;
import android.os.Handler;
import android.util.Log;
import android.media.MediaRecorder;
public class MainActivity extends AppCompatActivity {
TextView mStatusView;
private static double mEMA = 0.0;
static final private double EMA_FILTER = 0.6;
static String decider="0";
private dbRunnable newdb = new dbRunnable();
MediaRecorder recorder;
class dbRunnable implements Runnable{
@Override
public void run(){
Log.i("Runnable","called"); //Log to see if runnable is called successfully
Log.i("decider",decider); //decider is supposed to be "0" until stopRecord is called, but somehow becomes "1" before that
while(true){
Log.i("Runnable","called");
if(decider=="1"){
break;
}
}
}
}
public void startRecord(View view) {
Log.i("Inside", "start record");
if (recorder == null) {
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile("/dev/null");
try {
recorder.prepare();
} catch (java.io.IOException ioe) {
android.util.Log.e("[Monkey]", "IOException: " +
android.util.Log.getStackTraceString(ioe));
} catch (java.lang.SecurityException e) {
android.util.Log.e("[Monkey]", "SecurityException: " +
android.util.Log.getStackTraceString(e));
}
try {
recorder.start();
Thread dec1 = new Thread(newdb); //Creating new thread
dec1.start(); //Starting the runnable
} catch (java.lang.SecurityException e) {
android.util.Log.e("[Monkey]", "SecurityException: " +
android.util.Log.getStackTraceString(e));
}
}
}
public void stopRecord(View view){
if (recorder != null) {
recorder.stop();
recorder.release();
recorder = null;
}
decider="1";
Log.i("Hello","record complete");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void updateTv(){
mStatusView.setText(Double.toString((getAmplitudeEMA())) + " dB");
}
public double soundDb(double ampl){
return 20 * Math.log10(getAmplitudeEMA() / ampl);
}
public double getAmplitude() {
if (recorder != null)
return (recorder.getMaxAmplitude());
else
return 0;
}
public double getAmplitudeEMA() {
double amp = getAmplitude();
mEMA = EMA_FILTER * amp + (1.0 - EMA_FILTER) * mEMA;
return mEMA/100;
}
// Used to load the 'native-lib' library on application startup.
static {
System.loadLibrary("native-lib");
}
}