Android Camera unlock mediarecorder -


i want write app records video through smartphone camera. found website : http://developer.android.com/guide/topics/media/camera.html#custom-camera

im using source code started.

my main activity:

 public class mainactivity extends activity {          protected static final string tag = null;         private camera mcamera;         private camerapreview mpreview;         private mediarecorder mmediarecorder=null;         public static final int media_type_image = 1;         public static final int media_type_video = 2;         private boolean isrecording = false;           /** safe way instance of camera object. */         public static camera getcamerainstance(){             camera c = null;             try {                 c = camera.open(); // attempt camera instance             }             catch (exception e){                 // camera not available (in use or not exist)             }             return c; // returns null if camera unavailable         }          @override         public void oncreate(bundle savedinstancestate) {             super.oncreate(savedinstancestate);             setcontentview(r.layout.activity_main);              // create instance of camera             mcamera = getcamerainstance();              // create our preview view , set content of our activity.             mpreview = new camerapreview(this, mcamera);             framelayout preview = (framelayout) findviewbyid(r.id.camera_preview);             preview.addview(mpreview);            // add listener capture button          final button capturebutton = (button) findviewbyid(r.id.button_capture);          capturebutton.setonclicklistener(              new view.onclicklistener() {                  @override                  public void onclick(view v) {                      if (isrecording) {                          // stop recording , release camera                          mmediarecorder.stop();  // stop recording                          releasemediarecorder(); // release mediarecorder object                          mcamera.lock();         // take camera access mediarecorder                           // inform user recording has stopped                          capturebutton.settext("capture");                          //setcapturebuttontext("capture");                          isrecording = false;                      } else {                          // initialize video camera                          if (preparevideorecorder()) {                              // camera available , unlocked, mediarecorder prepared,                              // can start recording                              mmediarecorder.start();                               // inform user recording has started                              capturebutton.settext("stop");                              //setcapturebuttontext("stop");                              isrecording = true;                          } else {                              // prepare didn't work, release camera                              releasemediarecorder();                              // inform user                          }                      }                  }              }          );         }          @suppresslint("newapi") private boolean preparevideorecorder(){              mcamera = getcamerainstance();             log.d(tag, "mcamera object: " + mcamera.getnumberofcameras());             mmediarecorder = new mediarecorder();              // step 1: unlock , set camera mediarecorder             try{             mcamera.unlock();             }catch (runtimeexception r){         log.d(tag, "mcamera unlock: " + r.getmessage());             }             mmediarecorder.setcamera(mcamera);              // step 2: set sources             mmediarecorder.setaudiosource(mediarecorder.audiosource.mic);             mmediarecorder.setvideosource(mediarecorder.videosource.camera);              // step 3: set camcorderprofile (requires api level 8 or higher)             mmediarecorder.setprofile(camcorderprofile.get(camcorderprofile.quality_high));              // step 4: set output file             mmediarecorder.setoutputfile(getoutputmediafile(media_type_video).tostring());              // step 5: set preview output             mmediarecorder.setpreviewdisplay(mpreview.getholder().getsurface());              // step 6: prepare configured mediarecorder             try {                 mmediarecorder.prepare();             } catch (illegalstateexception e) {                 log.d(tag, "illegalstateexception preparing mediarecorder: " + e.getmessage());                 releasemediarecorder();                 return false;             } catch (ioexception e) {                 log.d(tag, "ioexception preparing mediarecorder: " + e.getmessage());                 releasemediarecorder();                 return false;             }             return true;         }           @override         protected void onpause() {             super.onpause();             releasemediarecorder();       // if using mediarecorder, release first             releasecamera();              // release camera on pause event         }          private void releasemediarecorder(){             if (mmediarecorder != null) {                 mmediarecorder.reset();   // clear recorder configuration                 mmediarecorder.release(); // release recorder object                 mmediarecorder = null;                 mcamera.lock();           // lock camera later use             }         }          private void releasecamera(){             if (mcamera != null){                 mcamera.release();        // release camera other applications                 mcamera = null;             }         }          /** create file saving image or video */         private static file getoutputmediafile(int type){             // safe, should check sdcard mounted             // using environment.getexternalstoragestate() before doing this.              file mediastoragedir = new file(environment.getexternalstoragepublicdirectory(                       environment.directory_pictures), "mycameraapp");             // location works best if want created images shared             // between applications , persist after app has been uninstalled.              // create storage directory if not exist             if (! mediastoragedir.exists()){                 if (! mediastoragedir.mkdirs()){                     log.d("mycameraapp", "failed create directory");                     return null;                 }             }              // create media file name             string timestamp = new simpledateformat("yyyymmdd_hhmmss").format(new date());             file mediafile;             if (type == media_type_image){                 mediafile = new file(mediastoragedir.getpath() + file.separator +                 "img_"+ timestamp + ".jpg");             } else if(type == media_type_video) {                 mediafile = new file(mediastoragedir.getpath() + file.separator +                 "vid_"+ timestamp + ".mp4");             } else {                 return null;             }              return mediafile;         }      } 

my camera preview class:

/** basic camera preview class */ public class camerapreview extends surfaceview implements surfaceholder.callback {     private surfaceholder mholder;     private camera mcamera;     private static final string tag = "preview";      public camerapreview(context context, camera camera) {         super(context);         mcamera = camera;          // install surfaceholder.callback notified when         // underlying surface created , destroyed.         mholder = getholder();         mholder.addcallback(this);         // deprecated setting, required on android versions prior 3.0         mholder.settype(surfaceholder.surface_type_push_buffers);     }      public void surfacecreated(surfaceholder holder) {         // surface has been created, tell camera draw preview.         try {             mcamera.setpreviewdisplay(holder);             mcamera.startpreview();         } catch (ioexception e) {             log.d(tag, "error setting camera preview: " + e.getmessage());         }     }      public void surfacedestroyed(surfaceholder holder) {         // empty. take care of releasing camera preview in activity.     }      public void surfacechanged(surfaceholder holder, int format, int w, int h) {         // if preview can change or rotate, take care of events here.         // make sure stop preview before resizing or reformatting it.          if (mholder.getsurface() == null){           // preview surface not exist           return;         }          // stop preview before making changes         try {             mcamera.stoppreview();         } catch (exception e){           // ignore: tried stop non-existent preview         }          // set preview size , make resize, rotate or         // reformatting changes here          // start preview new settings         try {             mcamera.setpreviewdisplay(mholder);             mcamera.startpreview();          } catch (exception e){             log.d(tag, "error starting camera preview: " + e.getmessage());         }     } } 

but when run app on samsung s5660 api 2.3.3 error:

06-07 19:55:48.859: d/(20274): mcamera object: 1 06-07 21:10:19.159: d/(20465): mcamera unlock: null 06-07 19:55:48.859: w/dalvikvm(20274): threadid=1: thread exiting uncaught exception (group=0x40018578) 06-07 19:55:48.859: e/androidruntime(20274): fatal exception: main 06-07 19:55:48.859: e/androidruntime(20274): java.lang.nullpointerexception 06-07 19:55:48.859: e/androidruntime(20274):    @ com.example.videocapture.mainactivity.preparevideorecorder(mainactivity.java:118) 06-07 19:55:48.859: e/androidruntime(20274):    @ com.example.videocapture.mainactivity.access$5(mainactivity.java:111) 06-07 19:55:48.859: e/androidruntime(20274):    @ com.example.videocapture.mainactivity$1.onclick(mainactivity.java:91) 06-07 19:55:48.859: e/androidruntime(20274):    @ android.view.view.performclick(view.java:2485) 06-07 19:55:48.859: e/androidruntime(20274):    @ android.view.view$performclick.run(view.java:9080) 06-07 19:55:48.859: e/androidruntime(20274):    @ android.os.handler.handlecallback(handler.java:587) 06-07 19:55:48.859: e/androidruntime(20274):    @ android.os.handler.dispatchmessage(handler.java:92) 06-07 19:55:48.859: e/androidruntime(20274):    @ android.os.looper.loop(looper.java:130) 06-07 19:55:48.859: e/androidruntime(20274):    @ android.app.activitythread.main(activitythread.java:3687) 06-07 19:55:48.859: e/androidruntime(20274):    @ java.lang.reflect.method.invokenative(native method) 06-07 19:55:48.859: e/androidruntime(20274):    @ java.lang.reflect.method.invoke(method.java:507) 06-07 19:55:48.859: e/androidruntime(20274):    @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:867) 06-07 19:55:48.859: e/androidruntime(20274):    @ com.android.internal.os.zygoteinit.main(zygoteinit.java:625) 06-07 19:55:48.859: e/androidruntime(20274):    @ dalvik.system.nativestart.main(native method) 

the null pointer exeception @ lin 118 preparevideorecorder():

// step 1: unlock , set camera mediarecorder         mcamera.unlock(); 

when app starts camera displayed , when hit button "capture" error thrown. think problem "mcamera = getcamerainstance();" called again in function , first 1 created in main activity camera preview class.

i added logtag after "mcamera = getcamerainstance();" in preparevideorecorder() see if null cann see report says "06-07 19:55:48.859: d/(20274): mcamera object: 1"

the solution remove line mcamera = getcamerainstance(); in preparevideorecorder() function.


Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -