如何使用MediaRecorder在Android上捕获视频? [英] How to use MediaRecorder to capture video on Android?
问题描述
我想使用MediaRecorder
创建一个简短的视频剪辑,但是我不知道如何使用它.
I would like to create a short video clip using the MediaRecorder
, but I don't know how to use it.
在清单文件中,我在application-end-tag之前添加了以下权限:
In my manifest file I added these permissions before the application-end-tag:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_VIDEO" />
</application>
当用户按下按钮时,我用以下代码创建MediaRecorder
:
And I create a MediaRecorder
with this code when the user press a button:
private void startRecording() {
mRecorder = new MediaRecorder();
mRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mRecorder.setOutputFile("myvideo.mpeg4");
try {
mRecorder.prepare();
} catch (IOException e) {
}
mRecorder.start();
}
但是,当我运行该代码时,出现强制关闭-应用程序意外停止.请重试"消息.我应该如何使用MediaRecorder
以及如何调试应用程序以查看导致异常的原因?我是否需要向清单文件添加任何其他用户权限?
But when I run that code I get a "Force close - The application has stopped unexpectedly. Please try again" message. How should I use MediaRecorder
and how can I debug my application to see what causes the exception? Do I need to add any other user permissions to my manifest file?
推荐答案
您必须设置预览显示,才能使用媒体记录器进行视频记录. 请参见 MediaRecorder教程
You MUST set a preview display in order to use the media recorder for video recording. See MediaRecorder tutorial
这篇关于如何使用MediaRecorder在Android上捕获视频?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!