Configuring and Controlling Video Recording

As described in the state model above, before recording you must specify the input sources, output format, audio and video encoder, and an output file —in that order.

The setAudioSource and setVideoSource methods let you specify a MediaRecorder.AudioSource or MediaRecorder.VideoSource static constant that define the audio and video source, respectively.

Once you've selected your input sources, select the output format using thesetOutputFormat method to specify a MediaRecorder.OutputFormat constant.

Use the set[audio/video]Encoder methods to specify an audio or video encoder constant from the MediaRecorder.[Audio/Video]Encoder class. Take this opportunity to set the frame rate or video output size if desired.

Finally, assign a file to store the recorded media using the setOutputFile method before calling prepare.

Listing 11-9 shows how to configure a Media Recorder to record audio and video from the microphone and camera using the default format and encoder to a file on the SD card.

LISTING 11-9: Configuring the Media Recorder

Available for download on MediaRecorder mediaRecorder = new MediaRecorder();

// Configure the input sources mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);

// Set the output format mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT); // Specify the audio and video encoding mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT); mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);

// Specify the output file mediaRecorder.setOutputFile("/sdcard/myoutputfile.mp4");

// Prepare to record mediaRecorder.prepare();

To begin recording, call the start method, as shown in this extension to Listing 11-9.


The setOutputFile method must be called before prepare and after setOutputFormat or it will throw an Illegal State Exception.

When you're finished, calktop to end the playback, followed by release to free the Media Recorder resources.

mediaRecorder.stop(); mediaRecorder.release();

Mobile Apps Made Easy

Mobile Apps Made Easy

Quick start guide to skyrocket your offline and online business success with mobile apps. If you know anything about mobile devices, you’ve probably heard that famous phrase coined by one of the mobile device’s most prolific creators proclaiming that there’s an app for pretty much everything.

Get My Free Training Guide

Post a comment