java - Front Camera Record Video Failed -
i trying record audio by front camera in 1 of activities using mediarecorder. part of code shown below.if open camera, works well. if open front camera, there no runtime error can't open output mp4. under corresponding path there generate new mp4 file, file size looks normally, 11.2m forexample. if press open it, shows "sorry, cann't open file".
part of cameraservice.java
@override public int onstartcommand(intent intent, int flags, int startid) { log.d("tag", "======= service in onstartcommand"); if (util.checkcamerahardware(this)) { mcamera = util.getcamerainstance(); if (mcamera != null) { surfaceview sv = new surfaceview(this); windowmanager wm = (windowmanager) getsystemservice(context.window_service); windowmanager.layoutparams params = new windowmanager.layoutparams(1, 1, windowmanager.layoutparams.type_system_overlay, windowmanager.layoutparams.flag_watch_outside_touch, pixelformat.translucent); surfaceholder sh = sv.getholder(); sv.setzorderontop(true); sh.setformat(pixelformat.transparent); sh.addcallback(new surfaceholder.callback() { @override public void surfacecreated(surfaceholder holder) { camera.parameters params = mcamera.getparameters(); mcamera.setparameters(params); camera.parameters p = mcamera.getparameters(); list<camera.size> listsize; listsize = p.getsupportedpreviewsizes(); camera.size mpreviewsize = listsize.get(2); log.v("tag", "preview width = " + mpreviewsize.width + " preview height = " + mpreviewsize.height); p.setpreviewsize(mpreviewsize.width, mpreviewsize.height); listsize = p.getsupportedpicturesizes(); camera.size mpicturesize = listsize.get(2); log.v("tag", "capture width = " + mpicturesize.width + " capture height = " + mpicturesize.height); p.setpicturesize(mpicturesize.width, mpicturesize.height); mcamera.setparameters(p); try { mcamera.setpreviewdisplay(holder); } catch (ioexception e) { e.printstacktrace(); } mcamera.startpreview(); mcamera.unlock(); mmediarecorder = new mediarecorder(); mmediarecorder.setcamera(mcamera); mmediarecorder.setaudiosource(mediarecorder.audiosource.camcorder); mmediarecorder.setvideosource(mediarecorder.videosource.camera); mmediarecorder.setprofile(camcorderprofile.get(1,camcorderprofile.quality_high)); mmediarecorder.setoutputfile(util.getoutputmediafile(util.media_type_video).getpath()); mmediarecorder.setpreviewdisplay(holder.getsurface()); try { mmediarecorder.prepare(); } catch (illegalstateexception e) { log.d("tag", "====== illegalstateexception preparing mediarecorder: " + e.getmessage()); } catch (ioexception e) { log.d("tag", "====== ioexception preparing mediarecorder: " + e.getmessage()); } mmediarecorder.start(); log.d("tag", "========= recording start"); new handler().postdelayed(new runnable() { @override public void run() { mmediarecorder.stop(); mmediarecorder.reset(); mmediarecorder.release(); mmediarecorder=null; mcamera.stoppreview(); mcamera.release(); log.d("tag", "========== recording finished."); } }, 10000); } @override public void surfacechanged(surfaceholder holder, int format, int width, int height) { } @override public void surfacedestroyed(surfaceholder holder) { } }); wm.addview(sv, params); } else { log.d("tag", "==== camera service failed"); } } else { log.d("tag", "==== there no camera hardware on device."); } return super.onstartcommand(intent, flags, startid); }
part of util.java
public static camera getcamerainstance() { camera c = null; try { c = camera.open(1); } catch (exception e) { log.d("tag", "open camera failed: " + e); } return c; }
update: change 2 lines shown below, switching camera , record video well.
cameraservice.java mmediarecorder.setprofile(camcorderprofile.get(1,camcorderprofile.quality_high));
=> mmediarecorder.setprofile(camcorderprofile.get(camcorderprofile.quality_high));
util.java c = camera.open(1);
=> c = camera.open();
i solved problem occasionally.
mmediarecorder.setprofile(camcorderprofile.get(1,camcorderprofile.quality_high));
==> mmediarecorder.setprofile(camcorderprofile.get(1,camcorderprofile.quality_low));
now works well. don't know why works , why did not work before?
Comments
Post a Comment