在Android上录制无声视频

12

在Android中,是否可以从摄像头录制视频时不包含音频流?

目标:减小输出文件大小。

2个回答

19
你可以通过从内置配置文件(CamcorderProfile)中复制所需字段来准备MediaRecorder。只需略过音频设置,你就可以开始了。编辑下面的代码以满足你的需求,这里的步骤3是必要的部分。

你可以通过从内置配置文件(CamcorderProfile)中复制所需字段来准备 MediaRecorder。只需略过音频设置,你就可以开始了。编辑下面的代码以满足你的需求,这里的步骤3是必要的部分。

private boolean prepareVideoRecorder() {

    mCamera = getCameraInstance();
    mMediaRecorder = new MediaRecorder();

    // store the quality profile required
    CamcorderProfile profile = CamcorderProfile.get(cameraid, CamcorderProfile.QUALITY_HIGH);

    // Step 1: Unlock and set camera to MediaRecorder
    mCamera.unlock();
    mMediaRecorder.setCamera(mCamera);

    // Step 2: Set sources
    mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);

    // Step 3: Set all values contained in profile except audio settings
    mMediaRecorder.setOutputFormat(profile.fileFormat);
    mMediaRecorder.setVideoEncoder(profile.videoCodec);
    mMediaRecorder.setVideoEncodingBitRate(profile.videoBitRate);
    mMediaRecorder.setVideoFrameRate(profile.videoFrameRate);
    mMediaRecorder.setVideoSize(profile.videoFrameWidth, profile.videoFrameHeight);

    // Step 4: Set output file
    mMediaRecorder.setOutputFile(getOutputMediaFile(MEDIA_TYPE_VIDEO).toString());

    // Step 5: Set the preview output
    mMediaRecorder.setPreviewDisplay(mPreview.getHolder().getSurface());

    // Step 6: Prepare configured MediaRecorder
    try {
        mMediaRecorder.prepare();
    } catch (IllegalStateException e) {
        releaseMediaRecorder();
        return false;
    } catch (IOException e) {
        releaseMediaRecorder();
        return false;
    }
    return true;
}

8

您可以在不调用setAudio*的情况下使用MediaRecorder。这是我第一次使用MediaRecorder,但这个例子似乎可以工作:

public class CamcorderView extends SurfaceView implements
        SurfaceHolder.Callback {

    private SurfaceHolder mHolder;

    private Camera mCamera;
    private MediaRecorder mRecorder;

    public CamcorderView(Context context, AttributeSet attrs) {
        super(context, attrs);

        mHolder = getHolder();
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        mHolder.addCallback(this);

        mCamera = Camera.open();
        mRecorder = new MediaRecorder();

    }

    public void stop() {
        mRecorder.stop();
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height) {
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        mCamera.unlock();
        mRecorder.setCamera(mCamera);

        mRecorder.setPreviewDisplay(mHolder.getSurface());

        // You may want to change these
        mRecorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
        mRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
        mRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);

        // You'll definitely want to change this
        mRecorder.setOutputFile("/mnt/sdcard/out");

        try {
            mRecorder.prepare();
        } catch (IllegalStateException e) {
            Log.e("IllegalStateException", e.toString());
        } catch (IOException e) {
            Log.e("IOException", e.toString());
        }
        mRecorder.start();

    }
}

您可能还需要调用以下方法:

  • setVideoSize(int, int);:设置视频尺寸
  • setVideoFrameRate(int);:设置视频帧率

1
谢谢!它真的有效!也许您知道如何使用CamcorderProfile应用这些设置?因为我正在使用自动生成的参数camcorderProfile = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);,然后所有设置都将应用为mediaRecorder.setProfile(camcorderProfile); - Sergii
你能提供更多信息吗?因为它没有正常工作。 - Teraiya Mayur
很遗憾,仅仅省略AudioSource在Android 10+上并不起作用;MediaRecorder.prepare()会失败。我一直在寻找适用于此API级别的方法,但没有成功。 - head in the codes
我没有安卓10+,所以无法提供帮助。也不会在一段时间内制作安卓应用程序。 - Joel Sjögren

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接