Activity whith pitch detect TarsosDSP no restart

316 Views Asked by At

I wrote a guitar tuner in music application. The main activity launch the tuner when click on button. The tuner works fine. But if I come back to main activity. The Tuner activity start, but pitch detection does not. The thread don't stop for the recorder.

I launch an activity Act_Accordage from the main activity with

public void clicAccordage(View v){
        Intent intentAct_Accordage = new Intent(this, Act_Accordeur .class);
    startActivity(intentAct_Accordage);
}

Here is my Act_Accordage activity

public final AudioDispatcher dispatcher = AudioDispatcherFactory.fromDefaultMicrophone(22050, 2048, 0);
    ....
          dispatcher.addAudioProcessor(new PitchProcessor(PitchEstimationAlgorithm.YIN, 22050, 2048, new PitchDetectionHandler() {

                @Override
                public void handlePitch(PitchDetectionResult pitchDetectionResult,
                                        AudioEvent audioEvent) {
                    final float pitchInHz = pitchDetectionResult.getPitch();
                    if (Logo.interrupted()) {
                        return;
                    }
                    if (pitchInHz > -1) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                // display name note and cursor ou UI thread
                               textFreqNote.setText(String.valueOf(pitchInHz) + " CREATE "); 
                               Affcurseur(pitchInHz);
                             });
                    }

                }
            }));
             new Thread(dispatcher, "Audio Dispatcher");

    2***/I come back main with  button or backbutton
    public void onclicSetBackMain(View v) {
            Act_Accordeur.this.finish();
            Toast.makeText(Act_Accordeur.this, "Guitare accordée", Toast.LENGTH_LONG).show();}
1

There are 1 best solutions below

0
On

Use dispatcher.stop(); to stop the Audio Dispatcher thread.It took me a long time in the official documentation but it was worth it.