surfaceview - Android: Application lost the surface when recording to dynamic object -
i in bit of pickle. trying display camera feed while view launched background service. getting runtime error
04-19 15:00:31.872 31125-31442/csce483.vbox e/recorder: starting prepare 04-19 15:00:31.872 31125-31442/csce483.vbox e/mediarecorderjni: application lost surface 04-19 15:00:31.872 31125-31442/csce483.vbox e/recorder: starting record 04-19 15:00:31.872 31125-31442/csce483.vbox e/mediarecorder: start called in invalid state: 4
when use dynamically created view. , static view not compatible window manager.
in oncreate():
try { mcamera = camera.open(); } catch (exception e) { e.printstacktrace(); } msurfaceview = (surfaceview) view.inflate(getbasecontext(),r.layout.camera,null); //msurfaceview = (surfaceview) findviewbyid(r.id.surfaceview); mholder = msurfaceview.getholder(); mholder.addcallback(this); mholder.settype(surfaceholder.surface_type_push_buffers); msurfaceview.setvisibility(view.invisible); yourbr = new myreceiver(); yourbr.setmainactivityhandler(this); intentfilter callinterceptorintentfilter = new intentfilter(); callinterceptorintentfilter.addaction("csce483.vbox.start"); callinterceptorintentfilter.addaction("csce483.vbox.stop"); registerreceiver(yourbr, callinterceptorintentfilter);
then in receiver:
public static class myreceiver extends broadcastreceiver { mainactivity yourmain = null; void setmainactivityhandler(mainactivity main){ yourmain = main; } @override public void onreceive(context context, intent intent) { string message = intent.getaction(); if(message.equalsignorecase("csce483.vbox.start")){ if(!yourmain.recording) { log.e("accel", "received crash!"); yourmain.msurfaceview.setvisibility(view.visible); yourmain.wm = (windowmanager) context.getsystemservice(window_service); windowmanager.layoutparams windowmanagerparams = new windowmanager.layoutparams(windowmanager.layoutparams.type_system_overlay); yourmain.wm.addview( yourmain.msurfaceview, windowmanagerparams); new thread(new runnable() { @override public void run() { yourmain.startrecording(); //record w/ new camera api } }).start(); } } else if(message.equalsignorecase("csce483.vbox.stop")){ log.e("accel", "recieved stop signal!"); yourmain.stoprecording(); } } }
starting recorder:
public void startrecording() { //all file code temporary until server working string timestamp = new simpledateformat("mmddyyyy_hhmmss").format(new date()); videofile = new file(environment.getexternalstoragedirectory(), "/pictures/"+timestamp+".mp4"); log.e("file", "saving to: " + videofile.getabsolutepath()); if (!videofile.exists()) { file parent = videofile.getparentfile(); if (parent != null) { try { videofile.createnewfile(); } catch (ioexception e) { e.printstacktrace(); } } } cameratriggered = true; if(!camerapresent()) return; if (mrecorder == null) { mrecorder = new mediarecorder(); } mcamera.unlock(); mrecorder.setcamera(mcamera); mrecorder.setpreviewdisplay(mholder.getsurface()); mrecorder.setvideosource(mediarecorder.videosource.camera); mrecorder.setoutputformat(mediarecorder.outputformat.mpeg_4); mrecorder.setvideoencoder(mediarecorder.videoencoder.mpeg_4_sp); mrecorder.setpreviewdisplay(mholder.getsurface()); if(this.getresources().getconfiguration().orientation != configuration.orientation_landscape) { mrecorder.setorientationhint(90); } else { mrecorder.setorientationhint(0); } mrecorder.setoutputfile(videofile.getabsolutepath()); try { log.e("recorder","starting prepare"); mrecorder.prepare(); } catch (exception e) { e.printstacktrace(); } try { log.e("recorder","starting record"); mrecorder.start(); } catch (exception e) { e.printstacktrace(); } }
thanks!
Comments
Post a Comment