0

我正在尝试使用 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);
    }
}
4

2 回答 2

2

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

在调用 prepare 之前,相机可能需要数百毫秒的时间来“设置”。

请参阅以下帖子:

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

于 2010-07-21T14:21:31.877 回答
1
recorder.setVideoSize(176, 144); 
recorder.setVideoFrameRate(20); 
recorder.setAudioChannels(1); 
recorder.setPreviewDisplay( mHolder.getSurface());//<- I believe this helped

这对我有帮助。在调试期间查看 logcat

于 2011-04-14T10:26:23.080 回答