Click here to Skip to main content
15,166,358 members
Articles / Artificial Intelligence / Machine Learning
Posted 4 Sep 2010


42 bookmarked

Recording and Playing Video on Android

, , ,
Rate me:
Please Sign up or sign in to vote.
3.10/5 (20 votes)
4 Sep 2010CPOL6 min read
This article attempts to elucidate how to create an application to record and play video. There is also a brief explanation of related APIs
Copyright © 2010 Tata Consultancy Services Ltd. All Rights Reserved


Early versions of Android platform did not support video recording and hence the developers were unable to create any video recording applications. With the release of SDK 1.5 (Cupcake) which is based on Linux Kernel 2.6.27, several new features and updates were introduced. This Android version supports video recording and playback.

Android 1.5(Cupcake) and later releases(Donut and Eclair) have APIs for recording video. But, it is difficult to find relevant, lucid and simple articles that explain how to use the Android APIs for recording video. This article attempts to elucidate how to create an application to record and play video. There is also a brief explanation of related APIs.

Video Playback

Android supports playing and recording video under Android Media package. The MediaPlayer Class in this package facilitates video playing.

Creating a media player in other technologies like j2me is more complicated as the customised view needs to be created, the progress slider needs to be handled, etc. However, Android framework makes playing video easy by providing VideoView and Media Controller classes. This additional abstraction handles most of the complications.

VideoView control encapsulates creation and instantiation of MediaPlayer. The VideoView class can load videos from various sources (such as resources or SD card). It takes care of computing measurement from the video so that it can be used in any layout manager, and provides various display options such as scaling and tinting.

MediaController is a view which adds control to the MediaPlayer like play, pause and the progress slider. It synchronizes the controls with the state of the MediaPlayer.

The below section discusses the development of the application for playing video file located on SD card.

Steps To Play Video

Step 1: Create a VideoView Widget

Add VideoView to your layout file.

<?xml version="1.0" encoding="utf-8"?>
android:layout_width="fill_parent" android:layout_height="fill_parent"
       android:layout_y="10px" />
Step 2: Add it as Content of Activity

Use setContentView method to set content of the activity to the video layout file we created.

protected void onCreate(Bundle savedInstanceState) {
          videoView = (VideoView)this.findViewById(;
Step 3: Set the Path of Video or URI

Use setVideoURI method to set path of the video, the path contains web address of video or location on the SD card.

videoView.setVideoURI(Uri.parse("Web Address"));


Note: To read the file from web, add the Internet permission to applications' manifest file.

(i.e. Add <uses-permission android:name="android.permission.INTERNET" /> to applications manifest file.)

Step 4: Set the Focus

Call setFocus method of VideoView object to bring VideoView object in focus:


You can also add MediaController to play, stop, pause and seek through video. Create an object of MediaController class and pass that object to VideoView objects setMediaController method.

MediaController mc = new MediaController(this);

Recording Video

This section explains how to record a video on Android. For recording video, MediaRecorder class will be used. To handle the display of camcorder, SurfaceView and SurfaceHolder.Callback interface will be used.

The below section throws light on MediaRecorder, SurfaceView and SurfaceHolder.Callback interface.

  1. MediaRecorder: This class is used to record audio and video. It has different methods to configure the recorder. The methods being used in the Video Recorder are explained below:
    • setVideoSource(): This sets the video source (i.e. Camera or Default) to be used for recording. If this method is not called, the output file will not contain a video track.
    • setOutputFormat(): This sets the format of the output file produced during recording. Call this after setAudioSource()/setVideoSource() but before prepare().
    • setVideoEncoder: This sets the video encoder to be used for recording. If this method is not called, the output file will not contain a video track. Call this after setOutputFormat() and before prepare().
    • setOutputFile(): This sets the path of the output file. Call this after setOutputFormat() but before prepare().
    • setPreviewDisplay(): Sets a Surface to show a preview of recorded media (video). Calls this before prepare() to make sure that the desirable preview display is set.
    • prepare(): Prepares the recorder to begin capturing and encoding data. This method must be called after setting up the desired audio and video sources, encoders, file format, etc., but before start().
    • start(): Begins capturing and encoding data to the file specified with setOutputFile(). Call this after prepare().

    (Note: These methods should be called in the sequence explained above.)

  2. SurfaceView: This is a customized view which provides a drawing surface inside the View hierarchy. It is used for resource intensive tasks where rapid updates or high frame rates are required such as 3D graphics, creating games, or previewing the camera in real time.

    The main advantage of using this view is that it provides an independent thread to update the view, which gives a great user experience.

  3. SurfaceHolder: As the name suggests, this interface holds the SurfaceView object. It allows developers to control the surface size and format, edit the pixels in the surface, and monitor changes to the surface.

Now we can use the above explained APIs to develop a VideoRecording application. We will begin with developing a customized view.

  • Create the RecorderPreview class which extends the SurfaceView and Implements the SurfaceHolder.Callback interface.
    class RecorderPreview extends SurfaceView implements SurfaceHolder.Callback
          //Create objects for MediaRecorder and SurfaceHolder.
          MediaRecorder recorder;
          SurfaceHolder holder;
          //Create constructor of Preview Class. In this, get an object of
          //surfaceHolder class by calling getHolder() method. After that add
          //callback to the surfaceHolder. The callback will inform when surface is
          //created/changed/destroyed. Also set surface not to have its own buffers.
          public Preview(Contect context,MediaRecorder temprecorder) {
          // Implement the methods of SurfaceHolder.Callback interface
          // SurfaceCreated : This method gets called when surface is created.
          // In this, initialize all parameters of MediaRecorder object as explained
          // above.
          public void surfaceCreated(SurfaceHolder holder){
    	    } catch (Exception e) {
    	    	String message = e.getMessage()

    (Note: To allow recording video, add permission into the application's manifest file. i.e. Add <uses-permission android:name="android.permission.RECORD_VIDEO"/>).

    // SurfaceDestroyed : This method gets called immediately before the
    // surface is being destroyed. Stop camera preview because surface will no
    // longer exist.
    public void surfaceDestroyed(SurfaceHolder holder)
    	recorder = null;
    //surfaceChanged : This method is called after the surface is created.
    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h)
  • Implement the onCreate method of video recording application.

    //Create objects of MediaRecorder and Preview class
    private MediaRecorder recorder;
    private Preview preview;
        // In this method, create an object of MediaRecorder class. Create an object of
        // RecorderPreview class(Customized View). Add RecorderPreview class object
        // as content of UI.
    public void onCreate(Bundle savedInstanceState)
        recorder = new MediaRecorder();
        preview = new RecorderPreview(this,recorder);
    //Create option menu which can be used for starting and stopping video recording.
    // Also implement the onOptionsItemSelected to handle the events for option menu.
    public boolean onCreateOptionsMenu(Menu menu)
        menu.add(0, 0, 0, "StartRecording");
        menu.add(0, 1, 0, "StopRecording");
        return super.onCreateOptionsMenu(menu);
    public boolean onOptionsItemSelected(MenuItem item)
        switch (item.getItemId())
            case 0:
            case 1:
        return super.onOptionsItemSelected(item);

The application is now ready for testing. Video recording is available only on handsets having version of SDK 1.5 or later. It is not possible to check application on emulator, you need to install it on handset.


In this article, we learned how to develop an application for video recording, playback and got an overview of related APIs. A sample application is included with this article. Hopefully, it is useful in developing a video component for your application.

Related Resources


Sandeep Andre

Sandeep Andre is a Bachelor in Computer Engineering, Pune University. He has worked as a research team member with Mumbai Innovation Labs, Tata Consultancy Services Limited. Currently he is doing his masters in Computer Science at University of North Carolina Charlotte. His research interests are Mobile Computing, Networking and Operating System.

Suneeta Chawla

Suneeta Chawla is MTech(IT), DAVV University. She is a research team member with Mumbai Innovation Labs, Tata Consultancy Services Limited. Her research interests are Mobile Computing.

Pankaj Doke

Pankaj Doke is a Bachelor in Computer Engineering, Mumbai University. He is a research team lead with Mumbai Innovation Labs, Tata Consultancy Services Limited. His research interests are Mobile computing, Computer Security, Pattern Recognition, Machine Learning and Large Scale Systems.

Sanjay Kimbahune

Sanjay Kimbahune is a Bachelor of Engineering in Electronics, Amravati University. He is a research scientist with Mumbai Innovation Labs, Tata Consultancy Services Limited. His research interests are Mobile Computing and Telecom Systems.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Authors

India India
No Biography provided

No Biography provided

Sandeep Andre
United States United States
No Biography provided

No Biography provided

Comments and Discussions

Questioncoding placement Pin
Member 1457261829-Aug-19 16:08
MemberMember 1457261829-Aug-19 16:08 
GeneralMy vote of 2 Pin
Member 1116343718-Oct-14 8:36
MemberMember 1116343718-Oct-14 8:36 
QuestionWhen I run this on my device I get a "Hello World" screen Pin
CoolSops15-Feb-13 5:53
MemberCoolSops15-Feb-13 5:53 
QuestionZoom in/out while recording... Pin
junaidas21-Nov-12 21:39
Memberjunaidas21-Nov-12 21:39 
Questioncode for live streaming video in android Pin
nagchowdary 3-Apr-12 3:02
Membernagchowdary 3-Apr-12 3:02 
AnswerRe: code for live streaming video in android Pin
nagchowdary 16-Apr-12 1:35
Membernagchowdary 16-Apr-12 1:35 
Questioncapturing and playing video with multitasking Pin
Ruby Bautista9-Mar-12 4:26
MemberRuby Bautista9-Mar-12 4:26 
AnswerRe: capturing and playing video with multitasking Pin
Member 1185012420-Jul-15 9:41
MemberMember 1185012420-Jul-15 9:41 
QuestionNot working Pin
Tobias Manthey10-Jul-11 3:39
MemberTobias Manthey10-Jul-11 3:39 
GeneralNo audio reception Pin
tauheed12913-Apr-11 3:58
Membertauheed12913-Apr-11 3:58 
GeneralRe: No audio reception Pin
kash5513-Apr-11 21:22
Memberkash5513-Apr-11 21:22 
GeneralThank YOU! Pin
kash556-Apr-11 22:16
Memberkash556-Apr-11 22:16 
GeneralMy vote of 1 Pin
Venkatesh Mookkan28-Mar-11 17:30
MemberVenkatesh Mookkan28-Mar-11 17:30 
Generalvery useful, thanks. Pin
David.Jinyu.Han29-Jan-11 22:39
MemberDavid.Jinyu.Han29-Jan-11 22:39 
GeneralMy vote of 5 Pin
chjungen22-Oct-10 8:49
Memberchjungen22-Oct-10 8:49 
GeneralMy vote of 3 Pin
Aoi Karasu 30-Sep-10 0:32
professional Aoi Karasu 30-Sep-10 0:32 
GeneralMy vote of 5 Pin
loopidio16-Sep-10 2:34
Memberloopidio16-Sep-10 2:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.