How do I use MediaRecorder to record video without causing a segmentation fault?

Posted by rabidsnail on Stack Overflow See other posts from Stack Overflow or by rabidsnail
Published on 2009-11-30T22:51:16Z Indexed on 2010/03/24 5:53 UTC
Read the original article Hit count: 624

Filed under:
|
|
|

I'm trying to use android.media.MediaRecorder to record video, and no matter what I do the android runtime segmentation faults when I call prepare(). Here's an example:

public void onCreate(Bundle savedInstanceState)
{
    Log.i("video test", "making recorder");
    MediaRecorder recorder = new MediaRecorder();
    contentResolver = getContentResolver();
    try {
        super.onCreate(savedInstanceState);
        Log.i("video test", "--------------START----------------");
        SurfaceView target_view = new SurfaceView(this);
        Log.i("video test", "making surface");
        Surface target = target_view.getHolder().getSurface();
        Log.i("video test", target.toString());
        Log.i("video test", "new recorder");
        recorder = new MediaRecorder(); 
        Log.i("video test", "set display");
        recorder.setPreviewDisplay(target);
        Log.i("video test", "pushing surface");
        setContentView(target_view);
        Log.i("video test", "set audio source");
        recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        Log.i("video test", "set video source");
        recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
        Log.i("video test", "set output format");
        recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        Log.i("video test", "set audio encoder");
        recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        Log.i("video test", "set video encoder");
        recorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
        Log.i("video test", "set max duration");
        recorder.setMaxDuration(3600); 
        Log.i("video test", "set on info listener");
        recorder.setOnInfoListener(new listener()); 
        Log.i("video test", "set video size");
        recorder.setVideoSize(320, 240); 
        Log.i("video test", "set video frame rate");
        recorder.setVideoFrameRate(15); 
        Log.i("video test", "set output file");
        recorder.setOutputFile(get_path(this, "foo.3gp"));
        Log.i("video test", "prepare");
        recorder.prepare(); 
        Log.i("video test", "start");
        recorder.start();
        Log.i("video test", "sleep");
        Thread.sleep(3600);
        Log.i("video test", "stop");
        recorder.stop();
        Log.i("video test", "release");
        recorder.release();
        Log.i("video test", "-----------------SUCCESS------------------");
        finish();
    } catch (Exception e) {
        Log.i("video test", e.toString());
        recorder.reset();
        recorder.release();
        Log.i("video tets", "-------------------FAIL-------------------");
        finish();
    }
}

public static String get_path (Context context, String fname) {
    String path = context.getFileStreamPath("foo").getParentFile().getAbsolutePath();
    String res = path+"/"+fname;
    Log.i("video test", "path: "+res);
    return res;
}

class listener implements MediaRecorder.OnInfoListener {
    public void onInfo(MediaRecorder recorder, int what, int extra) {
        Log.i("video test", "Video Info: "+what+", "+extra);
    }
}

© Stack Overflow or respective owner

Related posts about android

Related posts about video