Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

calling dispatcher.stop() #200

Open
ilyosbekkk opened this issue Feb 26, 2021 · 1 comment
Open

calling dispatcher.stop() #200

ilyosbekkk opened this issue Feb 26, 2021 · 1 comment

Comments

@ilyosbekkk
Copy link

Hi, I am implementing AudioDispatcher in my application, but I am not sure how to start and stop it properly, here is my code:


public class MyAudioFeatureRecorder {
private static final String TAG = "MyAudioFeatureRecorder";
Thread thread;
private boolean runThread;
private final AudioDispatcher dispatcher;
static int dataSourceId;
static SilenceDetector silenceDetector;
MyAudioProcessor mainProcessor;
SharedPreferences configPrefs;
Context context;
public static long PREVIOUS_TIME;
// endregion

public MyAudioFeatureRecorder(Context context) {


    this.context = context;
    configPrefs = context.getSharedPreferences("Configurations", Context.MODE_PRIVATE);
    if (DbMgr.getDB() == null)
        DbMgr.init(context);
    PREVIOUS_TIME = 0;
    int SAMPLING_RATE = 11025;
    int AUDIO_BUFFER_SIZE = 1024;
    dispatcher = AudioDispatcherFactory.fromDefaultMicrophone(SAMPLING_RATE, AUDIO_BUFFER_SIZE, 512);
    double SILENCE_THRESHOLD = -65.0D;
    silenceDetector = new SilenceDetector(SILENCE_THRESHOLD, false);
    dataSourceId = configPrefs.getInt("SOUND_DATA", -1);

    Log.d(TAG, "MyAudioFeatureRecorder: Service  is  Running...");
    mainProcessor = new MyAudioProcessor(configPrefs);


    if (dispatcher != null) {
        dispatcher.addAudioProcessor(silenceDetector);
        dispatcher.addAudioProcessor(mainProcessor);
    }
}



public void start() {
    thread = new Thread(() -> {


        while (runThread) {
            Log.e(TAG, "start: waiting");
            dispatcher.run();
          
        }

    });
    runThread = true;
    thread.start();
}

public void stop() {
    if (runThread) {
        runThread = false;
        thread.interrupt();
        try {
            dispatcher.stop() 
            Log.e(TAG, "stop: dispatcher stop");
            thread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Log.e("TAG", "stop: DeviceModel Stop");
    }
}

private static class MyAudioProcessor implements be.tarsos.dsp.AudioProcessor {
    SharedPreferences confPrefs;

    public MyAudioProcessor(SharedPreferences confPrefs) {
        this.confPrefs = confPrefs;
    }

    @Override
    public boolean process(AudioEvent audioEvent) {
        long nowTime = System.currentTimeMillis();

        if (silenceDetector.currentSPL() >= -110.0D) {
            if (dataSourceId != -1 && nowTime >= PREVIOUS_TIME + 10 * 1000) {
                Log.e(TAG, "process: SoundData  is  saving");
                DbMgr.saveMixedData(dataSourceId, nowTime, 1.0f, nowTime, silenceDetector.currentSPL(), "ENERGY");
        
                PREVIOUS_TIME = System.currentTimeMillis();
            }
        }
        return true;
    }

    @Override
    public void processingFinished() {
    }
}

}


In my Android application, the current SPL should be saved only once in 10 seconds, it is working correctly but when I call the stop() function (interrupts the running thread) UI is freezing and I know that UI is freezing because of improper use of dispatcher.stop(). Where exactly should I call dispatcher.stop() to properly stop the dispatcher?

Thank you

@zukarusan
Copy link

I might be a latecomer. But you might want to try to use a thread to stop the dispatcher

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants