I know this question was asked so many times before.
I don't want to add a flag in the Run()
method. because the method still runs and drains battery.
How can I stop the Thread completely?!
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_layout);
AudioDispatcher dispatcher = AudioDispatcherFactory.fromDefaultMicrophone(22050,1024,0);
dispatcher.addAudioProcessor(new PitchProcessor(PitchEstimationAlgorithm.AMDF, 22050, 1024, new PitchDetectionHandler() {
@Override
public void handlePitch(PitchDetectionResult pitchDetectionResult,
AudioEvent audioEvent) {
final float pitchInHz = pitchDetectionResult.getPitch();
runOnUiThread(new Runnable() {
@Override
public void run() {
Log.i("" + pitchInHz);
}
});
}
}));
Thread thread = new Thread(dispatcher,"Audio Dispatcher");
thread.start();
}