Android mediarecorder recording video tips for start failed solutions

Source: Internet
Author: User

Turn from 49247411

Today test Mediarecorder recording function when there is an egg pain bug, according to the official sample written well, start unexpectedly error, print the log a look said: Start failed.

The code is as follows:

[Java]View PlainCopy
  1. Mcamera = Camera.open (0);
  2. Surfaceview = New Surfaceview (this);
  3. Setcontentview (Surfaceview);
  4. Msurfaceholder = Surfaceview.getholder ();
  5. Msurfaceholder.settype (surfaceholder.surface_type_push_buffers);
  6. Mrecorder = new Mediarecorder ();
  7. Mrecorder.setcamera (Mcamera);
  8. Mrecorder.setaudiosource (Audiosource.default);
  9. Mrecorder.setvideosource (Videosource.default);
  10. Mrecorder.setoutputformat (Outputformat.mpeg_4);
  11. Mrecorder.setaudioencoder (MediaRecorder.AudioEncoder.DEFAULT);
  12. Mrecorder.setvideoencoder (MediaRecorder.VideoEncoder.DEFAULT);
  13. Mrecorder.setvideosize ( 240);
  14. Mrecorder.setvideoframerate (20);
  15. Mrecorder.setpreviewdisplay (Msurfaceholder.getsurface ());
  16. File File = new file (Environment.getexternalstoragedirectory ()
  17. . GetAbsolutePath () + "/test.mp4");
  18. if (File.exists ())
  19. {
  20. File.delete ();
  21. }
  22. Try
  23. {
  24. File.createnewfile ();
  25. } catch (IOException E1)
  26. {
  27. E1.printstacktrace ();
  28. }
  29. Mrecorder.setoutputfile (File.getabsolutepath ());
  30. Msurfaceholder.addcallback (new Callback ()
  31. {
  32. @Override
  33. public void surfacedestroyed (Surfaceholder holder)
  34. {
  35. }
  36. @Override
  37. public void surfacecreated (Surfaceholder holder)
  38. {
  39. Try
  40. {
  41. Mrecorder.prepare ();
  42. Mrecorder.start ();
  43. } catch (Exception e)
  44. {
  45. Writer writer = new StringWriter ();
  46. PrintWriter printwriter = new PrintWriter (writer);
  47. E.printstacktrace (PrintWriter);
  48. Throwable cause = E.getcause ();
  49. While (cause! = null)
  50. {
  51. Cause.printstacktrace (PrintWriter);
  52. Cause = Cause.getcause ();
  53. }
  54. String str = writer.tostring ();
  55. System.out.println (str);
  56. }
  57. }
  58. @Override
  59. public void surfacechanged (Surfaceholder holder, int format,
  60. int width, int height)
  61. {
  62. }
  63. });

In general, this is because the system does not support you set the resolution rate of these parameters, so you first need to check whether the system supports the parameters you set, parameters can be judged by the camera's parameter, called as follows:

[Java]View PlainCopy
    1. Mcamera.getparameters (). getsupportedvideosizes ()

But sometimes even if the correct parameters are set, you may need to call the following method before you start:

[Java]View PlainCopy
    1. Mcamera.unlock ();

This method from the method name is unlocked camera estimation is afraid to use the device conflict, the specific reason for later to see the camera related API description. So I think the best thing to do in this case is to do the following after acquiring the camera object:

[Java]View PlainCopy
    1. Mcamera.stoppreview ();
    2. Mcamera.setpreviewcallback (null);
    3. Mcamera.setpreviewcallbackwithbuffer (null);
    4. Mcamera.release ();
    5. Mcamera = null;
    6. Mcamera = Camera.open (0);
    7. Mcamera.lock ();
    8. Mcamera.unlock ();

Android mediarecorder recording video tips for start failed solutions

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.