Android录制没有音频的视频

时间:2012-08-12 08:42:51

标签: android android-camera mediarecorder android-mediarecorder

在Android中是否可以在没有音频流的情况下录制来自Camera的视频?

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

2 个答案:

答案 0 :(得分:11)

您可以通过复制内置配置文件(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;
}

答案 1 :(得分:7)

您可以在不调用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);