android - Tap and hold to record video like Vine -


i want make app record video, seems vine, hold record, release stop, hold record , keep end.

i have used mediarecorder, record once time, if start record again, app crashed.

please tell me there way this? edited code:

public class videorecordingactivity extends appcompatactivity implements view.ontouchlistener, view.onlongclicklistener { private context mycontext; private boolean hascamera; private boolean onrecording;  private camera mcamera; private camerapreview mpreview; private mediarecorder mediarecorder; private boolean camerafront = false; private int cameraid;  private int videonumer; private boolean isactiondown = false;  @override public void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_video_introduction_recording);     initui();     initialize(); } private linearlayout lncamerapreview; private imagebutton btn_recording;  private void initui() {     lncamerapreview = (linearlayout) findviewbyid(r.id.ln_body_recording);     btn_recording = (imagebutton) findviewbyid(r.id.btn_recording);  }  public void initialize() {     mycontext = this;     mpreview = new camerapreview(this, cameraid, mcamera);     lncamerapreview.addview(mpreview);     btn_recording.setonlongclicklistener(this);     btn_recording.setontouchlistener(this);     videonumer = 0; } public boolean onlongclick(view v) {     isactiondown = true;     try {         boolean isprepared = false;         if (isactiondown)             isprepared = preparemediarecorder();         if (isprepared && isactiondown) {             // work on uithread better performance             runonuithread(new runnable() {                 public void run() {                     mediarecorder.start();                     onrecording = true;                 }             });          }     } catch (exception e) {         e.printstacktrace();         log.e("onlongpress error ", e.tostring());     }      return true; }   @override public boolean ontouch(view v, motionevent event) {     switch (event.getaction()) {         case motionevent.action_up:             isactiondown = false;              try {                 if (onrecording) {                      if (mediarecorder != null) {                         mediarecorder.stop();                     }                     onrecording = false;                     videonumer++;                 }             } catch (illegalstateexception e) {                 e.printstacktrace();             } catch (exception e) {                 e.printstacktrace();             }             break;     }     return false; }    public void onresume() {     super.onresume();     if (!hascamera(mycontext)) {         toast.maketext(mycontext, "sorry, phone not have camera!", toast.length_long).show();         return;     }     initcamera(); }  @override protected void onpause() {     super.onpause();     // when on pause, release camera in order used other     // applications     releasecamera(); }   private final int cmaxrecorddurationinms = 30000; private final long cmaxfilesizeinbytes = 5000000; private final int cframerate = 20; private file prrecordedfile;  @suppresslint("sdcardpath") private boolean preparemediarecorder() {     mediarecorder = new mediarecorder();      try {         mcamera.unlock();     } catch (exception ex) {         return false;     }          // adjust camera way need         mediarecorder.setcamera(mcamera);     mediarecorder.setaudiosource(mediarecorder.audiosource.default);     mediarecorder.setvideosource(mediarecorder.videosource.default);          //         camcorderprofile cphigh = camcorderprofile.get(camcorderprofile.quality_high);         mediarecorder.setprofile(cphigh);         mediarecorder.setpreviewdisplay(mpreview.getholder().getsurface());      mediarecorder.setoutputfile("/sdcard/" + videonumer + "videocapture_example.mp4");      //set max size     mediarecorder.setmaxduration(600000); // set max duration 60 sec.     mediarecorder.setmaxfilesize(50000000); // set max file size 50m      try {         mediarecorder.prepare();     } catch (exception e) {         releasemediarecorder();         e.printstacktrace();     }     return true; }  private void releasemediarecorder() {     if (mediarecorder != null) {         mediarecorder.reset(); // clear recorder configuration         mediarecorder.release(); // release recorder object         mediarecorder = null;         if (mcamera != null) {             mcamera.lock(); // lock camera later use         }     } }  /**  * camera  */  private void initcamera() {     if (mcamera == null) {         // if front facing camera not exist         if (findfrontfacingcamera() < 0) {             toast.maketext(this, "no front facing camera found.", toast.length_long).show();         }         mcamera = camera.open(findbackfacingcamera());         mpreview.refreshcamera(mcamera);     }     onrecording = false; }  private boolean hascamera(context context) {     // check if device has camera     if (context.getpackagemanager().hassystemfeature(packagemanager.feature_camera)) {         hascamera = true;     } else {         hascamera = false;     }     return hascamera; }  private int findfrontfacingcamera() {     int cameraid = -1;     // search front facing camera     int numberofcameras = camera.getnumberofcameras();     (int = 0; < numberofcameras; i++) {         camera.camerainfo info = new camera.camerainfo();         camera.getcamerainfo(i, info);         if (info.facing == camera.camerainfo.camera_facing_front) {             cameraid = i;             camerafront = true;             break;         }     }     this.cameraid = cameraid;     return cameraid; }  private int findbackfacingcamera() {     int cameraid = -1;     // search facing camera     // number of cameras     int numberofcameras = camera.getnumberofcameras();     // every camera check     (int = 0; < numberofcameras; i++) {         camera.camerainfo info = new camera.camerainfo();         camera.getcamerainfo(i, info);         if (info.facing == camera.camerainfo.camera_facing_back) {             cameraid = i;             camerafront = false;             break;         }     }     this.cameraid = cameraid;     return cameraid; }  public void switchcamera() {     // if camera preview front     if (camerafront) {         int cameraid = findbackfacingcamera();         if (cameraid >= 0) {             // open backfacingcamera             mcamera = camera.open(cameraid);             // refresh preview             mpreview.refreshcamera(mcamera);         }     } else {         int cameraid = findfrontfacingcamera();         if (cameraid >= 0) {             // open backfacingcamera             mcamera = camera.open(cameraid);             // refresh preview             mpreview.refreshcamera(mcamera);         }     } }  private void releasecamera() {     // stop , release camera     if (mcamera != null) {         mcamera.release();         mcamera = null;     } } 

}

you can achieve functionality setting onlongclicklistener() , ontouchlistener() on record button. this:

recordbtn.setonlongclicklistener(recordbtnlclistener); recordbtn.setontouchlistener(recordbtntouchlistener); 

then :

@override public boolean onlongclick(view v) {     ivcancel.setvisibility(view.gone);     ivdone.setvisibility(view.gone);     isactiondown = true;     try {         if (isactiondown) {             initrecorder();             if (isactiondown)                 preparerecorder();         }         if (isprepared && isactiondown) {             mmediarecorder.start();             isrecording = true;         }     } catch (exception e) {         e.printstacktrace();         log.e("onlongpress error ", e.tostring());     }      return true; } 

and :

@override public boolean ontouch(view v, motionevent event) {     switch (event.getaction()) {         case motionevent.action_up:             isactiondown = false;              try {                 if (isrecording) {                      if (mmediarecorder != null) {                         mmediarecorder.stop();                     }                     isrecording = false;                 }             } catch (illegalstateexception e) {                 e.printstacktrace();             } catch (exception e) {                 e.printstacktrace();             }             break;     }     return false; } 

so, in way can record parts of video.means each time longpress record button, recording starts. , time release button, recording stops , here have save part of video in temporary folder. once done taking parts of videos many want, have combine parts of videos make single video.

here, code merge video parts saved in temperory folder:

public void mergevideos() {     try {         list<movie> inmovies = new arraylist<>();         (int = 0; < videospathlist.size(); i++) {             string filepath = videospathlist.get(i);             try {                 movie movie = moviecreator.build(filepath);                 if (movie != null)                     inmovies.add(movie);             } catch (exception e) {                 e.printstacktrace();             }         }         list<track> videotracks = new linkedlist<track>();         list<track> audiotracks = new linkedlist<track>();         (movie m : inmovies) {             (track t : m.gettracks()) {                 try {                     if (t.gethandler().equals("soun")) {                         audiotracks.add(t);                     }                     if (t.gethandler().equals("vide")) {                         videotracks.add(t);                     }                 } catch (exception e) {                  }             }         }         movie result = new movie();         if (audiotracks.size() > 0) {             result.addtrack(new appendtrack(audiotracks                     .toarray(new track[audiotracks.size()])));         }         if (videotracks.size() > 0) {             result.addtrack(new appendtrack(videotracks                     .toarray(new track[videotracks.size()])));         }         basiccontainer out = (basiccontainer) new defaultmp4builder().build(result);         file f = null;         string finalvideopath;         try {             f = setupvideofile(environment         .getexternalstoragedirectory()+"/myapp/videos/");             finalvideopath = f.getabsolutepath();          } catch (ioexception e) {             e.printstacktrace();             f = null;             finalvideopath = null;         }         writablebytechannel fc = new randomaccessfile(finalvideopath, "rw").getchannel();         out.writecontainer(fc);         fc.close();         deletefilesdir(); //in method have delete parts of video stored in temporary folder.       } catch (exception e) {         e.printstacktrace();        progressdialog.dismiss();         finish();     } }  file setupvideofile(string directory) throws ioexception {     file videofile = null;     if (environment.media_mounted.equals(environment             .getexternalstoragestate())) {         file storagedir = new file(directory);         if (storagedir != null) {             if (!storagedir.mkdirs()) {                 if (!storagedir.exists()) {                     log.d("camerasample", "failed create directory");                     return null;                 }             }         }         videofile = file.createtempfile("video_"                         + system.currenttimemillis() + "_",                 .mp4, storagedir);     }     return videofile; } 

you can call mergevideos() method after stopping mediarecorder.

hope code helps you. :)

for merging videos have use isoparser library. have add following dependendy in gradle file :

compile 'com.googlecode.mp4parser:isoparser:1.0.5.4' 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -