如何使用MediaRecorder录制视频而不会导致分段错误?

时间:2009-11-30 22:51:17

标签: android video camera mediarecorder

我正在尝试使用android.media.MediaRecorder来录制视频,无论我做什么,我调用prepare()时都会运行android运行时分段错误。这是一个例子:

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

2 个答案:

答案 0 :(得分:2)

问题似乎与相机状态机的时间有关。

在调用准备工作之前,相机可能需要数百毫秒来“设置”。

请参阅以下帖子:

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

答案 1 :(得分:1)

recorder.setVideoSize(176, 144); 
recorder.setVideoFrameRate(20); 
recorder.setAudioChannels(1); 
recorder.setPreviewDisplay( mHolder.getSurface());//<- I believe this helped
这对我有帮助。在调试期间查看logcat