Code example for MediaRecorder: Recording audio and video

Methods: preparereleaseresetsetCamerasetOrientationHintsetOutputFilesetPreviewDisplaysetProfilesetVideoSourcestart

0
		super.onResume(); 
	} 
	 
	private void startVideoRecording(){ 
		if (prepareVideoRecorder()){ 
			mMediaRecorder.start();
			isRecording = true;
			Log.i(TAG, "Started the recording");
			 
			Timer mTimer = new Timer();
			mTimer.schedule(new TimerTask(){
				@Override 
				public void run(){ 
					stopVideoRecording(); 
				} 
			}, length * 1000);
		}else{ 
			releaseMediaRecorder(); 
			Log.e(TAG, "Couldn't prepare the mMediaRecorder" + mMediaRecorder);
		}		 
	} 
	 
	private void stopVideoRecording(){ 
		if (isRecording){
			mMediaRecorder.stop();
			releaseMediaRecorder(); 
			mCamera.lock();
			 
			Log.i(TAG, "Stopped the recording");
			isRecording = false;
		}else{ 
			Log.e(TAG, "Unable to stop video recording for mMediaRecorder " + mMediaRecorder);
		} 
		createNotification(); 
		Intent intent = new Intent(this, MainActivity.class);
		startActivity(intent);		
	} 
	 
	private boolean prepareVideoRecorder(){ 
		Log.i(TAG, "Preparing video recorder");
		mMediaRecorder = new MediaRecorder();
		filename = getOutputMediaFile(MEDIA_TYPE_VIDEO).toString();
		 
		mCamera.unlock();
		mMediaRecorder.setCamera(mCamera);
		 
		mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
		 
		mMediaRecorder.setProfile(CamcorderProfile.get(mCameraId, CamcorderProfile.QUALITY_TIME_LAPSE_HIGH));
		mMediaRecorder.setOrientationHint(270);
		 
		mMediaRecorder.setOutputFile(filename);
		mMediaRecorder.setPreviewDisplay(mSurfaceView.getHolder().getSurface());
 
//		AndroidHelpers.setCameraDisplayOrientation(ViewAction.this, mCameraId, mCamera); 
		 
		try{ 
			mMediaRecorder.prepare();
			Log.i(TAG, "mMediaRecorder has been prepared");
		}catch (IllegalStateException e){
			Log.e(TAG, "IllegalStateException preparing MediaRecorder: " + e.getMessage());
			releaseMediaRecorder(); 
			return false; 
		}catch (IOException e){
			Log.e(TAG, "IOException preparing MediaRecorder: " + e.getMessage());
			releaseMediaRecorder(); 
			return false; 
		} 
		return true; 
	} 
	 
	@Override 
	protected void onPause(){ 
		super.onPause(); 
		releaseMediaRecorder(); 
		releaseCamera(); 
	} 
	 
	protected void releaseMediaRecorder(){ 
		if (mMediaRecorder != null){
			mMediaRecorder.reset();
			mMediaRecorder.release();
			mMediaRecorder = null;
			mCamera.lock();
		} 
	}