Code example for MediaRecorder

Methods: preparesetAudioSourcesetCamerasetOutputFilesetProfilesetVideoSource

1
  private SurfaceHolder holder;
  private MediaRecorder mediaRecorder;
   
  private void prepareVideoCamera() throws IllegalStateException, IOException { 
    // Create a new Media Recorder. 
    mediaRecorder = new MediaRecorder();
     
    /** 
     * Listing 15-30: Preparing to record audio and video using the Media Recorder 
     */ 
    // Unlock the Camera to allow the Media Recorder to own it. 
    camera.unlock();
 
    // Assign the Camera to the Media Recorder. 
    mediaRecorder.setCamera(camera);
 
    // Configure the input sources. 
    mediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
    mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
 
    // Set the recording profile. 
    CamcorderProfile profile = null;
 
    if (CamcorderProfile.hasProfile(CamcorderProfile.QUALITY_1080P))
      profile = CamcorderProfile.get(CamcorderProfile.QUALITY_1080P);
    else if (CamcorderProfile.hasProfile(CamcorderProfile.QUALITY_720P))
      profile = CamcorderProfile.get(CamcorderProfile.QUALITY_720P);
    else if (CamcorderProfile.hasProfile(CamcorderProfile.QUALITY_480P))
      profile = CamcorderProfile.get(CamcorderProfile.QUALITY_480P);
    else if (CamcorderProfile.hasProfile(CamcorderProfile.QUALITY_HIGH))
      profile = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
       
    if (profile != null)
      mediaRecorder.setProfile(profile); 
 
    // Specify the output file 
    mediaRecorder.setOutputFile("/sdcard/myvideorecording.mp4");
 
    // Prepare to record 
    mediaRecorder.prepare();
  } 
   
  @Override 
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);