0

I am working at a flashlight app with stroboscope , at this moment the app is working, the flash is working with a button, and the frequency of the strobe is being incresed with the help of a seekbar.

The problem is that if the flash is on and i drag to the right on the seekbar to start and increse feqency of the strobe effect, but if I turn off the flash and drag on the seekbar at a certain point greaten than 0 and turn on the flash the strobe effect it works, but still when is on it will just not update and increase the freqency, and i have to turn off and onn again flashlight to change the freqency or to stop the strobe effect (of course when the flash is off move the seekBar).

Can you help me and make me understand why it does not update with the flash on, plesase! I would be grateful ! Thanks!

Here is the code :

SeekBar

SeekBar sbar = (SeekBar) findViewById(R.id.seekBar2);
    sbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            freq = progress;

        }

        public void onStartTrackingTouch(SeekBar seekBar) {


        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
        }
    });
}

StroboRunner

private class StroboRunner implements Runnable{
    int freq;
    boolean stopRunning = false;
    volatile int delay = 100;
    volatile int delayoff = 200;

    @Override
    public void run() {
        Camera.Parameters paramsOn = camera.getParameters();
        Camera.Parameters paramsOff = parameter;
        paramsOn.setFlashMode(Parameters.FLASH_MODE_TORCH);
        paramsOff.setFlashMode(Parameters.FLASH_MODE_OFF);

        try{
            while(!stopRunning) {

                    MainActivity.this.camera.setParameters(paramsOn);
                    MainActivity.this.camera.startPreview();
                    Thread.sleep(delay - freq);

                    // We make the thread sleeping
                    MainActivity.this.camera.setParameters(paramsOff);
                    MainActivity.this.camera.startPreview();
                    Thread.sleep(delayoff-freq);

            }

        } catch (Exception e){

        }

    }
}

Also for on and off of the flashlight I am using

 private boolean turnOnOff(boolean on) {

    if (on) {
        if (freq != 0) {
            SunetON();
            isFlashLightOn = true;
            sr = new StroboRunner();
            sr.freq = freq;
            t = new Thread(sr);
            t.start();
            return on;
        }
        else
            parameter.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
            SunetON();
    }
    if (!on) {
        if (t != null) {
            SunetOff();
            sr.stopRunning = true;
            isFlashLightOff = true;
            t = null;
            return on;
        }
        else
            parameter.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
        SunetOff();
    }
    camera.setParameters(parameter);
    camera.startPreview();
    return on;
}

the onClickListener

flashLight.setOnClickListener(new View.OnClickListener() {
         @Override
           public void onClick(View v) {
           isChecked = !isChecked;
           if (isChecked)
               flashLight.setImageResource(R.drawable.on);
           else
                flashLight.setImageResource(R.drawable.off);
                turnOnOff(isChecked);
          }
    });
VC.One
  • 14,790
  • 4
  • 25
  • 57

1 Answers1

1

EDIT: Nevermind, bad if/else formatting -- turnOnOff was already outside

A good place to start is to move turnOnOff(isChecked) outside of your else statement.

flashLight.setOnClickListener(new View.OnClickListener() {
         @Override
           public void onClick(View v) {
           isChecked = !isChecked;
           if (isChecked)
               flashLight.setImageResource(R.drawable.on);
           else
                flashLight.setImageResource(R.drawable.off);
          }

          turnOnOff(isChecked);
    });
Aidan Hoolachan
  • 2,285
  • 1
  • 11
  • 14
  • Yes, but if i move it outside my else statement, i will get an error(invalid method decalration; return type required | missing method body, or declare abstract) – Bucur Mihai May 06 '17 at 23:38