java - How to create a flash light as well as strobe effect. Need help to fix strobe effect -
i trying create flash light application contains strobe function. have achieved flash light on/off effect running trouble strobe effect. whenever try create strobe effect application crashes. if remove that, works fine.
i have 1 activity seekbar
@ top , on/off switch @ center. want achieve if user clicks on/off button flash light should toggle on/off if seekbar
@ top @ 0. if user increase value in seekbar
flash light should start strobe accordingly , if user move value 0 flashlight should remain constant. please me fix error.
log cat
07-23 16:54:12.610: w/dalvikvm(21210): threadid=11: thread exiting uncaught exception (group=0x40c15a68) 07-23 16:54:12.610: e/androidruntime(21210): fatal exception: thread-2373 07-23 16:54:12.610: e/androidruntime(21210): java.lang.runtimeexception: fail connect camera service 07-23 16:54:12.610: e/androidruntime(21210): @ android.hardware.camera.native_setup(native method) 07-23 16:54:12.610: e/androidruntime(21210): @ android.hardware.camera.<init>(camera.java:365) 07-23 16:54:12.610: e/androidruntime(21210): @ android.hardware.camera.open(camera.java:340) 07-23 16:54:12.610: e/androidruntime(21210): @ com.shyam.flashlight.strobecontroller.run(strobecontroller.java:29) 07-23 16:54:12.610: e/androidruntime(21210): @ java.lang.thread.run(thread.java:856)
flashlight.java
public class flashlight extends activity { imagebutton btnswitch; private camera camera; private boolean isflashon; private boolean hasflash; parameters params; mediaplayer mp; strobecontroller runner; thread bw; @suppresswarnings("deprecation") @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); requestwindowfeature(window.feature_no_title); setcontentview(r.layout.activity_flash_light); btnswitch = (imagebutton) findviewbyid(r.id.btnswitch); runner = strobecontroller.getinstance(); bw = new thread(runner); bw.start(); hasflash = getapplicationcontext().getpackagemanager().hassystemfeature(packagemanager.feature_camera_flash); if (!hasflash) { alertdialog alert = new alertdialog.builder(flashlight.this).create(); alert.settitle("error"); alert.setmessage("sorry, device doesn't support flash light!"); alert.setbutton("ok", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int which) { finish(); } }); alert.show(); return; } getcamera(); togglebuttonimage(); btnswitch.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { /*if (isflashon) { turnoffflash(); } else { turnonflash(); }*/ if (isflashon) { bw = new thread(runner); bw.start(); isflashon = false; } else { isflashon = true; runner.requeststop = true; } } }); final seekbar skbar = (seekbar) findviewbyid(r.id.volume_bar); skbar.setonseekbarchangelistener(new onseekbarchangelistener() { @override public void onstoptrackingtouch(seekbar seekbar) { } @override public void onstarttrackingtouch(seekbar seekbar) { } @override public void onprogresschanged(seekbar seekbar, int progress, boolean fromuser) { runner.delay = 101 - progress; runner.delayoff = 101 - progress; } }); } private void getcamera() { if (camera == null) { try { camera = camera.open(); params = camera.getparameters(); } catch (runtimeexception e) { log.e("camera error. failed open. error: ", e.getmessage()); } } } private void turnonflash() { if (!isflashon) { if (camera == null || params == null) { return; } playsound(); params = camera.getparameters(); params.setflashmode(parameters.flash_mode_torch); camera.setparameters(params); camera.startpreview(); isflashon = true; togglebuttonimage(); } } private void turnoffflash() { if (isflashon) { if (camera == null || params == null) { return; } playsound(); params = camera.getparameters(); params.setflashmode(parameters.flash_mode_off); camera.setparameters(params); camera.stoppreview(); isflashon = false; togglebuttonimage(); } } private void playsound() { if (isflashon) { mp = mediaplayer.create(flashlight.this, r.raw.light_switch_off); } else { mp = mediaplayer.create(flashlight.this, r.raw.light_switch_on); } mp.setoncompletionlistener(new oncompletionlistener() { @override public void oncompletion(mediaplayer mp) { mp.release(); } }); mp.start(); } private void togglebuttonimage() { if (isflashon) { btnswitch.setimageresource(r.drawable.btn_switch_on); } else { btnswitch.setimageresource(r.drawable.btn_switch_off); } } @override public void onbackpressed() { super.onbackpressed(); params = camera.getparameters(); params.setflashmode(parameters.flash_mode_off); camera.setparameters(params); camera.stoppreview(); isflashon = false; if (camera != null) { camera.release(); camera = null; } log.d("camera", "back pressed"); } }
strobecontroller.java
public class strobecontroller implements runnable { protected strobecontroller() { } public static strobecontroller getinstance() { return (instance == null ? instance = new strobecontroller() : instance); } private static strobecontroller instance; public volatile boolean requeststop = false; public volatile boolean isrunning = false; public volatile int delay = 10; public volatile int delayoff = 500; public volatile string errormessage = ""; @override public void run() { if (isrunning) return; requeststop = false; isrunning = true; camera cam = camera.open(); parameters pon = cam.getparameters(), poff = cam.getparameters(); pon.setflashmode(camera.parameters.flash_mode_torch); poff.setflashmode(camera.parameters.flash_mode_off); while (!requeststop) { try { cam.setparameters(pon); thread.sleep(delay); cam.setparameters(poff); thread.sleep(delayoff); } catch (interruptedexception ex) { } catch (runtimeexception ex) { requeststop = true; errormessage = "error setting camera flash status. device may unsupported."; } } cam.release(); isrunning = false; requeststop = false; } }
i notice exception being thrown @ line 29:
at com.shyam.flashlight.strobecontroller.run(strobecontroller.java:29)
is line 29 camera cam = camera.open();
?
you have correct permissions described here? also, trying open null camera? @ line 29 in strobecontroller class.
Comments
Post a Comment