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
- Mcamera = Camera.open (0);
- Surfaceview = New Surfaceview (this);
- Setcontentview (Surfaceview);
- Msurfaceholder = Surfaceview.getholder ();
- Msurfaceholder.settype (surfaceholder.surface_type_push_buffers);
- Mrecorder = new Mediarecorder ();
- Mrecorder.setcamera (Mcamera);
- Mrecorder.setaudiosource (Audiosource.default);
- Mrecorder.setvideosource (Videosource.default);
- Mrecorder.setoutputformat (Outputformat.mpeg_4);
- Mrecorder.setaudioencoder (MediaRecorder.AudioEncoder.DEFAULT);
- Mrecorder.setvideoencoder (MediaRecorder.VideoEncoder.DEFAULT);
- Mrecorder.setvideosize ( 240);
- Mrecorder.setvideoframerate (20);
- Mrecorder.setpreviewdisplay (Msurfaceholder.getsurface ());
- File File = new file (Environment.getexternalstoragedirectory ()
- . GetAbsolutePath () + "/test.mp4");
- if (File.exists ())
- {
- File.delete ();
- }
- Try
- {
- File.createnewfile ();
- } catch (IOException E1)
- {
- E1.printstacktrace ();
- }
- Mrecorder.setoutputfile (File.getabsolutepath ());
- Msurfaceholder.addcallback (new Callback ()
- {
- @Override
- public void surfacedestroyed (Surfaceholder holder)
- {
- }
- @Override
- public void surfacecreated (Surfaceholder holder)
- {
- Try
- {
- Mrecorder.prepare ();
- Mrecorder.start ();
- } catch (Exception e)
- {
- Writer writer = new StringWriter ();
- PrintWriter printwriter = new PrintWriter (writer);
- E.printstacktrace (PrintWriter);
- Throwable cause = E.getcause ();
- While (cause! = null)
- {
- Cause.printstacktrace (PrintWriter);
- Cause = Cause.getcause ();
- }
- String str = writer.tostring ();
- System.out.println (str);
- }
- }
- @Override
- public void surfacechanged (Surfaceholder holder, int format,
- int width, int height)
- {
- }
- });
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
- 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
- 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
- Mcamera.stoppreview ();
- Mcamera.setpreviewcallback (null);
- Mcamera.setpreviewcallbackwithbuffer (null);
- Mcamera.release ();
- Mcamera = null;
- Mcamera = Camera.open (0);
- Mcamera.lock ();
- Mcamera.unlock ();
Android mediarecorder recording video tips for start failed solutions