Search code examples
androidandroid-cameramediarecorderandroid-mediarecorder

Android record video without audio


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

Goal: to reduce the output file size.


Solution

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