我正在使用 MediaRecorder 录制视频。我的代码在 2.3.3 上运行良好,但在 4.0.3 上失败。
问题如下:代码 mediaRecorder.stop() 抛出 RuntimeExeption
java.lang.RuntimeException: stop failed.
at android.media.MediaRecorder.stop(Native Method)
带有 LogCat 消息
04-05 15:10:51.815: E/MediaRecorder(15709): stop failed: -1007
更新
我发现,MediaPlayer 几乎在启动后立即报告错误(通过 MediaPlayer.OnErrorListener)。错误代码是 100(媒体服务器死机),额外的 -1007。
更新 2 准备 MediaRecorder 的代码
c = Camera.open();
...
// Step 1: Unlock and set camera to MediaRecorder
camera.unlock();
mediaRecorder.setCamera(camera);
// Step 2: Set sources
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
// Step 3: Set a CamcorderProfile (requires API Level 8 or higher)
CamcorderProfile profile = CamcorderProfile
.get(CamcorderProfile.QUALITY_HIGH);
// manual set up!
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mediaRecorder.setVideoEncodingBitRate(profile.videoBitRate);
mediaRecorder.setVideoFrameRate(profile.videoFrameRate);
mediaRecorder.setVideoSize(profile.videoFrameWidth,
profile.videoFrameHeight);
mediaRecorder.setAudioChannels(profile.audioChannels);
mediaRecorder.setAudioEncodingBitRate(profile.audioBitRate);
mediaRecorder.setAudioSamplingRate(profile.audioSampleRate);
mediaRecorder.setAudioEncoder(profile.audioCodec);
//mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
mediaRecorder.setVideoEncoder(profile.videoCodec);
// mediaRecorder.setProfile(profile);
// Step 4: Set output file
mediaRecorder.setOutputFile("somefile.mp4");
// Step 5: Set the preview output
mediaRecorder.setPreviewDisplay(preview.getHolder().getSurface());
// Step 6: Prepare configured MediaRecorder
try {
mediaRecorder.prepare();
} catch ...
{ release mediaRecorder}
然后我只需调用 mediaRecorder.start() 请注意,我需要将视频编码为 mp4 格式。此代码适用于 Samsng Galaxy GIO (android 2.3.3) 并且如 Acer E305 (android 4.0.2) 所述失败
有任何想法吗?谢谢。