如何在 Android 上使用 MediaRecorder 捕获视频?
我想使用 MediaRecorder
创建一个短视频剪辑,但我不知道如何使用它。
在我的清单文件中,我在应用程序结束标记之前添加了这些权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_VIDEO" />
</application>
当用户按下按钮时,我使用此代码创建一个 MediaRecorder
:
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
以及如何调试我的应用程序以查看导致异常的原因?我需要向我的清单文件添加任何其他用户权限吗?
I would like to create a short video clip using the MediaRecorder
, but I don't know how to use it.
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>
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();
}
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先在您的清单文件中声明权限...
然后在您的主应用程序代码中...
First declare the permissions in your Manifest file...
Then in your main app code...
// 查看视频
// View the Video