12

Is it possible in Android to record video from Camera without audio stream?

Goal: to reduce the output file size.

Sergii
  • 1,521
  • 3
  • 24
  • 37

2 Answers2

18

You can prepare MediaRecorder by copying the required fields from inbuilt profile (CamcorderProfile). Just leave out the audio settings and you should be good to go. Edit code below for your needs, step 3 is the essential part here.

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;
}
Nishant Singh
  • 645
  • 7
  • 16
7

You can use a MediaRecorder without calling setAudio* on it. This is my first time using MediaRecorder, but this example seems to work:

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();

    }
}

You may also want to call:

  • setVideoSize(int, int);
  • setVideoFrameRate(int);
Joel Sjögren
  • 2,010
  • 1
  • 13
  • 12
  • 1
    Thanks! It really works! Maybe you know how to apply this settings with CamcorderProfile? Because I'm using automatically generated params as `camcorderProfile =CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);` , then all settings are applied as `mediaRecorder.setProfile(camcorderProfile); ` – Sergii Aug 12 '12 at 16:09
  • can you provide more info because its not work proper. – Teraiya Mayur Sep 10 '15 at 08:39
  • Simply leaving out the `AudioSource` unfortunately doesn't work on Android 10+; `MediaRecorder.prepare()` fails. I've been searching without success for a method which works on this API level. – head in the codes Feb 20 '21 at 02:04
  • I don't have android 10+ so I can't help. Not making android apps for a while either. – Joel Sjögren Feb 20 '21 at 07:31