Android MediaRecorder start failed: -12

2019-08-06 22:10发布

问题:

I've been trying to make an app (API 8) that records video, without audio. I've followed the instructions on the Android tutorial. My code is as follows:

    mCamera.unlock();
    recorder = new MediaRecorder();
    recorder.setCamera(mCamera);
    recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
    recorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
    recorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
    recorder.setOutputFile(getOutputMediaFile(MEDIA_TYPE_VIDEO).toString());
           //getOutputMediaFile returns a file path where the video will be saved
    recorder.setPreviewDisplay(mHolder.getSurface());
    try {
        recorder.prepare();
        recorder.start();
        recording = true;
    } catch (IllegalStateException e) {
        System.out.println("Error preparing recorder");
        e.printStackTrace();
    } catch (IOException e) {
        System.out.println("Error preparing video output");
        e.printStackTrace();
    }

When it reaches recorder.start(), the program crashes, throwing an IllegalStateException that can't be caught, along with an error code start failed: -12. The only other info I could find about this error code was in this post, but it doesn't give a very clear answer, nor is the model I'm using any of the ones addressed in the post.

Can anyone help identify what the error is and suggest a solution?

回答1:

So now I´ve tried an example and downloaded the sources. I modified it with Your code and it works without saving sound on API 8. Here is the main class:

    public class AndroidVideoCapture extends Activity implements SurfaceHolder.Callback{

Button myButton;
MediaRecorder mediaRecorder;
SurfaceHolder surfaceHolder;
boolean recording;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    recording = false;

    mediaRecorder = new MediaRecorder();
    initMediaRecorder();

    setContentView(R.layout.main);

    SurfaceView myVideoView = (SurfaceView)findViewById(R.id.videoview);
    surfaceHolder = myVideoView.getHolder();
    surfaceHolder.addCallback(this);
    surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

    myButton = (Button)findViewById(R.id.mybutton);
    myButton.setOnClickListener(myButtonOnClickListener);
}

private Button.OnClickListener myButtonOnClickListener 
= new Button.OnClickListener(){

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        if(recording){
            mediaRecorder.stop();
            mediaRecorder.release();
            finish();
        }else{
            mediaRecorder.start();
            recording = true;
            myButton.setText("STOP");
        }
    }};

@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
    // TODO Auto-generated method stub

}
@Override
public void surfaceCreated(SurfaceHolder arg0) {
    // TODO Auto-generated method stub
    prepareMediaRecorder();
}
@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
    // TODO Auto-generated method stub

}

private void initMediaRecorder(){

    mediaRecorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
    mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
    mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
    mediaRecorder.setOutputFile("/sdcard/myvideo.mp4");
}

private void prepareMediaRecorder(){
    mediaRecorder.setPreviewDisplay(surfaceHolder.getSurface());
    try {
        mediaRecorder.prepare();
    } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

}

The difference to Your code is, that in this tutorial no camera is instantiated. It only works with MediaRecorder. The other difference is, that the prepare() and start() methods are separated. The MediaRecorder is initialized at starting the app, prepared when SurfaceView is created, and started at button click. Maybe You should try to seperate them too and don´t use camera. Please try it that way and give me a feedback if it works.



回答2:

Have You tried to set MediaRecorder Profile?

    recorder.setProfile(CamcorderProfile.get(camId, CamcorderProfile.QUALITY_LOW));

There is a similar question here in sof at:

MediaRecorder "start failed -12"