Video recording with media recorder

2019-02-21 00:35发布

问题:

I am writing code for Video Recording function with Media recorder., But it throws Io Exception,Illegal State Exception i searched many times in google but no proper explanation for this one my code is

public class Simple extends Activity {
    /** Called when the activity is first created. */
    Button button,button2;
    MediaRecorder recorder;
    Camera camera;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        button=(Button) findViewById(R.id.button1);
        button2=(Button) findViewById(R.id.button2);

    camera=Camera.open();
    Camera.Parameters params = camera.getParameters();
    camera.setParameters(params);


    button.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub

            Toast.makeText(getBaseContext(), "Video Start", Toast.LENGTH_LONG).show();

            String mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
            mFileName += "/temp.3mp4";

            recorder=new MediaRecorder();


            recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
            recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
            recorder.setVideoFrameRate(10);
            recorder.setVideoSize(200, 300);
            recorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
            recorder.setMaxDuration(50000);
            recorder.setMaxFileSize(5000000);
            recorder.setOutputFile(mFileName);
            try {
                recorder.prepare();
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                Toast.makeText(getBaseContext(),e.toString(), Toast.LENGTH_LONG).show();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                Toast.makeText(getBaseContext(),e.toString(), Toast.LENGTH_LONG).show();
            }
            }
    });

    button2.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Toast.makeText(getBaseContext(), "Video Stop", Toast.LENGTH_LONG).show();
            recorder.stop();
        }
    });

}

}

回答1:

Here is the code for recording video...

recorder = new MediaRecorder();
        recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
        recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        recorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
        mPreview = new Preview(VideoRecorder.this,recorder);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        setContentView(mPreview);

recorder.start();

and here is the Preview Class....

class Preview extends SurfaceView implements SurfaceHolder.Callback
{
    SurfaceHolder mHolder;
    MediaRecorder tempRecorder;
    public Preview(Context context,MediaRecorder recorder) {
        super(context);
        tempRecorder=recorder;
        mHolder=getHolder();
        mHolder.addCallback(this);
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        // TODO Auto-generated constructor stub
    }

    public Surface getSurface()
    {
        return mHolder.getSurface();
    }

    public void surfaceCreated(SurfaceHolder holder){
        tempRecorder.setOutputFile("/sdcard/recordvideooutput.3gpp");
        tempRecorder.setPreviewDisplay(mHolder.getSurface());
        try{
            tempRecorder.prepare();
        } catch (Exception e) {
            String message = e.getMessage();
            tempRecorder.release();
            tempRecorder = null;
        }
    }

    public void surfaceDestroyed(SurfaceHolder holder) 
    {
        if(tempRecorder!=null)
        {
            tempRecorder.release();
            tempRecorder = null;
        }
    }

    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) 
    {

    }
}