Troubleshoot an Android mediarecorder recording video with Too short a problem _android

Source: Internet
Author: User

Specific performance:

Call Mediarecorder's start () and stop () interval cannot be less than 1 seconds (sometimes more than 1 seconds), otherwise it will collapse.

Error message:

Java.lang.RuntimeException:stop failed.
  At Android.media.MediaRecorder.stop (Native method)

Solution:

Call Setonerrorlistener (null) before stop; it's OK!

Related code:

 /** Start recording/@Override public MediaPart Startrecord () {if (Mmediaobject!= null && msurfaceholder!= nul

      L &&!mrecording) {MediaPart result = Mmediaobject.buildmediapart (Mcameraid, ". mp4");
          try {if (Mmediarecorder = = null) {Mmediarecorder = new Mediarecorder ();
        Mmediarecorder.setonerrorlistener (this);
        else {mmediarecorder.reset ();
        }//step 1:unlock and set camera to Mediarecorder Camera.unlock ();
        Mmediarecorder.setcamera (camera);

        Mmediarecorder.setpreviewdisplay (Msurfaceholder.getsurface ());
        Step 2:set sources Mmediarecorder.setvideosource (MediaRecorder.VideoSource.CAMERA)//before Setoutputformat () Mmediarecorder.setaudiosource (MediaRecorder.AudioSource.MIC);//before Setoutputformat () mmediarecorder.se

        Toutputformat (MediaRecorder.OutputFormat.MPEG_4); Set the format and encoding of the video output camcorderprofile mprofile = CamcorDerprofile.get (camcorderprofile.quality_480p);
        Mmediarecorder.setprofile (Mprofile); Mmediarecorder.setvideosize (640),//after Setvideosource (), after Setoutformat () Mmediarecorder.setaudioencodin
        Gbitrate (44100);
        if (Mprofile.videobitrate > 2 * 1024 * 1024) mmediarecorder.setvideoencodingbitrate (2 * 1024 * 1024);
        else Mmediarecorder.setvideoencodingbitrate (mprofile.videobitrate); Mmediarecorder.setvideoframerate (mprofile.videoframerate);//after Setvideosource (), after SetOutFormat () MMediaRec Order.setaudioencoder (MediaRecorder.AudioEncoder.AAC);//after Setoutputformat () Mmediarecorder.setvideoencoder (

        MediaRecorder.VideoEncoder.H264);//after Setoutputformat ()//mmediarecorder.setvideoencodingbitrate (800);

        Step 4:set Output File Mmediarecorder.setoutputfile (Result.mediapath); Step 5:set the preview output//Mmediarecorder.setorientaTionhint (90)//Add HTC Mobile phone will have problems LOG.E ("Yixia", "outputfile:" + Result.mediapath);
        Mmediarecorder.prepare ();
        Mmediarecorder.start ();
        Mrecording = true;
      return result;
        catch (IllegalStateException e) {e.printstacktrace ();
      LOG.E ("Yixia", "Startrecord", e);
        catch (IOException e) {e.printstacktrace ();
      LOG.E ("Yixia", "Startrecord", e);
        catch (Exception e) {e.printstacktrace ();
      LOG.E ("Yixia", "Startrecord", e);
  } return null;
    /** Stop recording/@Override public void Stoprecord () {Long endtime = System.currenttimemillis ();
      if (Mmediarecorder!= null) {//set does not collapse after mmediarecorder.setonerrorlistener (null);
      Mmediarecorder.setpreviewdisplay (NULL);
      try {mmediarecorder.stop ();
      catch (IllegalStateException e) {LOG.W ("Yixia", "Stoprecord", e); catch (RuntimeException e) {LOG.W ("Yixia", "Stoprecord"), e);
      catch (Exception e) {LOG.W ("Yixia", "Stoprecord", e);
      } if (camera!= null) {try {camera.lock ();
      catch (RuntimeException e) {log.e ("Yixia", "Stoprecord", e);
  } mrecording = false;
    /** Release Resources */@Override public void releases () {super.release ();
      if (Mmediarecorder!= null) {Mmediarecorder.setonerrorlistener (null);
      try {mmediarecorder.release ();
      catch (IllegalStateException e) {LOG.W ("Yixia", "Stoprecord", e);
      catch (Exception e) {LOG.W ("Yixia", "Stoprecord", e);
  } Mmediarecorder = null; @Override public void OnError (Mediarecorder mr, int what, int extra) {try {if (Mr!= null) MR.R
    ESET ();
    catch (IllegalStateException e) {LOG.W ("Yixia", "Stoprecord", e);
    catch (Exception e) {LOG.W ("Yixia", "Stoprecord", e); } if (Monerrorlistener!= null) MonerroRlistener.onvideoerror (what, extra); }

Above is to the Android Mediarecorder data collation, follow-up continue to add, the need for friends can refer to.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.