Working with Video Android

In recent years, video has become commonplace on handsets. Most handsets on the market now can record and play back video, and this is no different with Android, although the specific video features might vary from handset to handset.

Recording Video

Android applications can record video using the MediaRecorder class. Using MediaRecorder is a matter of following a few simple steps:

  1. Instantiate a new MediaRecorder object.
  2. Set the video source.
  3. Set the video output format.
  4. Set the video size to record (optional).
  5. Set the video frame rate (optional).
  6. Set the video encoder.
  7. Set the file to record to. (The extension must match output format.)
  8. Set the preview surface.
  9. Prepare the object for recording.
  10. Start the recording.
  11. Stop and release the recording object when finished.

Using some standard button controls, you can create an Activity to record and play back video using the preceding steps. The onClick() method for a record button might look like this:

public void onClick(View v) {
if (videoRecorder == null) {
videoRecorder = new MediaRecorder();
String pathForAppFiles =
pathForAppFiles += RECORDED_FILE;
MediaRecorder.OutputFormat.MPEG4 );
videoRecorder.setVideoSize(640, 480);
// button handling and other behavior here

The videoRecorder object is instantiated and given some video configuration values for the recording source. There are several values for each video configuration setting; however, supported values can vary by device.

A stop button configured with an onClick() handler might look like this:

public void onClick(View v) {
if (videoRecorder!= null) {
videoRecorder = null;
// button handling and other behavior here

Finally, applications wanting to record video require the explicit permission android .permission. CAMERA set within the AndroidManifest.xml file.

Now it is time to add the playback functionality, so we can watch the video we just recorded.

Playing Video

The simplest way to play back video with the Android SDK is to use the VideoView widget along with the MediaController widget to provide basic video controls. The following is an implementation of an onCreate() method within an Activity that demonstrates a workable video playback solution:

protected void onCreate(Bundle savedInstanceState) {
VideoView vv = (VideoView) findViewById(; MediaController mc = new MediaController(this);
Uri video = Uri.parse(MOVIE_URL);

A simple layout file with these controls might look like Figure. The MediaController presents a nice ProgressBar that shows download completion and current location. The use of the setAnchorView() method of the MediaController is not needed when used with the setMediaController() method of VideoView—it’s automatically set to the VideoView.

Screen showing video playback with default media controller displayed.

Screen showing video playback with default media controller displayed.

The call to the setVideoURI() method automatically starts playback. You can create a listener for when playback finishes using the setOnCompletionListener() method of the ViewView. The VideoView object has several other helpful methods, such as getDuration() and direct control over playback through methods such as pause(). For finer control over the media, or for an alternate way to play back media, you can use the MediaPlayer object. Use of it is similar to using the Camera—you need a Surface Holder.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd Protection Status

Android Topics