Code example for MediaRecorder: Recording audio and video

Methods: preparesetAudioEncodersetAudioSamplingRatesetAudioSourcesetCamerasetMaxDurationsetOutputFilesetOutputFormatsetPreviewDisplaysetVideoEncoder

0
			parameters.setPreviewSize(640, 480);
			mCamera.setParameters(parameters);
			mCamera.startPreview();
			mCamera.unlock();
 
			mVideoRecorder = new MediaRecorder();
			mVideoRecorder.setCamera(mCamera);
 
			// Media recorder setup is based on Listing 9-6, Hashimi et all 2010 
			// values based on best practices and good quality, 
			// tested via upload to YouTube and played in QuickTime on Mac Snow 
			// Leopard 
			mVideoRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
			mVideoRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
			mVideoRecorder
					.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);// THREE_GPP
																			// is 
																			// big-endian, 
																			// storing 
																			// and 
																			// transferring 
																			// the 
																			// most 
																			// significant 
																			// bytes 
																			// first. 
																			// MPEG_4 
																			// as 
																			// another 
																			// option 
			mVideoRecorder.setVideoSize(640, 480);// YouTube recommended size:
													// 320x240, 
													// OpenGazer eye tracker: 
													// 640x480 
													// YouTube HD: 1280x720 
			mVideoRecorder.setVideoFrameRate(20); // might be auto-determined
													// due to lighting 
			mVideoRecorder.setVideoEncodingBitRate(mVideoQuality);// 3000000=3
																	// megapixel, 
			// or the max of 
			// the camera 
			mVideoRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);// MPEG_4_SP
																			// Simple 
																			// Profile 
																			// is 
																			// for 
																			// low 
																			// bit 
																			// rate 
																			// and 
																			// low 
																			// resolution 
																			// H264 
																			// is 
																			// MPEG-4 
																			// Part 
																			// 10 
																			// is 
																			// commonly 
																			// referred 
																			// to 
																			// as 
																			// H.264 
																			// or 
																			// AVC 
			int sdk = android.os.Build.VERSION.SDK_INT;
			// Gingerbread and up can have wide band ie 16,000 hz recordings 
			// (Okay quality for human voice) 
			if (sdk >= 10) {
				mVideoRecorder
						.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_WB);
				mVideoRecorder.setAudioSamplingRate(16000);
			} else { 
				// Other devices only have narrow band, ie 8,000 hz 
				// (Same quality as a phone call, not really good quality for 
				// any purpose. 
				// For human voice 8,000 hz means /f/ and /th/ are 
				// indistinguishable) 
				mVideoRecorder
						.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
			} 
			mVideoRecorder.setMaxDuration(600000); // limite to 10min
													// 600,000limit to 30 
													// seconds 30,000 
			mVideoRecorder.setPreviewDisplay(holder.getSurface());
			mVideoRecorder.setOutputFile(mAudioResultsFile);
			mVideoRecorder.prepare();
			mVideoRecorder.start();
			mRecording = true;
		} catch (Exception e) {
			Log.e(TAG, e.toString());
 
		}