views:

444

answers:

2

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);
    }
}
A: 

Hi,

could u build this application?

Sweety
A: 

The issue appears to be related to timing of the state machine for the camera.

The camera may need hundreds of milliseconds to 'set up' before the prepare is called.

See the following post:

http://code.google.com/p/android/issues/detail?id=5050

ajaxlex