如何在 Android 上使用 MediaRecorder 捕获视频?

发布于 2024-11-29 18:16:49 字数 936 浏览 2 评论 0原文

我想使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

流年已逝 2024-12-06 18:16:49

首先在您的清单文件中声明权限...

<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.RECORD_VIDEO" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.Camera"/>

然后在您的主应用程序代码中...

Camera cam = Camera.open();
cam.unlock();

if(recorder == null) { recorder=new MediaRecorder(); }

recorder.setCamera(camera);
recorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
recorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));
recorder.setOutputFile(fileName);
recorder.setPreviewDisplay(holder.getSurface());

try{ recorder.prepare(); recorder.start(); } 
catch( IOException e ) { e.printStackTrace(); }

First declare the permissions in your Manifest file...

<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.RECORD_VIDEO" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.Camera"/>

Then in your main app code...

Camera cam = Camera.open();
cam.unlock();

if(recorder == null) { recorder=new MediaRecorder(); }

recorder.setCamera(camera);
recorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
recorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));
recorder.setOutputFile(fileName);
recorder.setPreviewDisplay(holder.getSurface());

try{ recorder.prepare(); recorder.start(); } 
catch( IOException e ) { e.printStackTrace(); }
七婞 2024-12-06 18:16:49

要宣传您的应用程序依赖于相机,请在清单文件中添加一个标签:

<manifest ... >
<uses-feature android:name="android.hardware.camera"
              android:required="true" />
...

使用相机应用程序录制视频

    static final int REQUEST_VIDEO_CAPTURE = 1;

 private void dispatchTakeVideoIntent() {
        Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
        if (takeVideoIntent.resolveActivity(getPackageManager()) != null) {
            startActivityForResult(takeVideoIntent, REQUEST_VIDEO_CAPTURE);
        }
    }

// 查看视频

 @Override
       protected void onActivityResult(int requestCode, int resultCode,Intent intent) {
    if (requestCode == REQUEST_VIDEO_CAPTURE && resultCode == RESULT_OK) 
    {
        Uri videoUri = intent.getData();
        mVideoView.setVideoURI(videoUri);
    }
}

To advertise that your application depends on having a camera, put a tag in the manifest file:

<manifest ... >
<uses-feature android:name="android.hardware.camera"
              android:required="true" />
...

Record a Video with a Camera App

    static final int REQUEST_VIDEO_CAPTURE = 1;

 private void dispatchTakeVideoIntent() {
        Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
        if (takeVideoIntent.resolveActivity(getPackageManager()) != null) {
            startActivityForResult(takeVideoIntent, REQUEST_VIDEO_CAPTURE);
        }
    }

// View the Video

 @Override
       protected void onActivityResult(int requestCode, int resultCode,Intent intent) {
    if (requestCode == REQUEST_VIDEO_CAPTURE && resultCode == RESULT_OK) 
    {
        Uri videoUri = intent.getData();
        mVideoView.setVideoURI(videoUri);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文