Android Video Recording API–Illustrated with an Example
Android has made video recording very simply by providing a few high level classes, including Camera, SurfaceView and MediaRecorder. This tutorial illustrates the Video Recording API by providing a workable example as shown in the screenshots.
Figure 1. Screenshots of Android Video Capturing Sample Program
The Camera class allows us to access the Camera on an Android device; the SurfaceView class provides a drawing surface, it is used to present a live preview in our application; the MediaRecoder contains the API to configure and record the video, it uses Camera class to access the hardware camera.
0. Live Preview Before Recording
Before users start to record video, they want to see what they’re capturing. That’s what the preview function does, it allows one to see what the camera is able to capture. The preview implementation is briefly illustrated as the code below,
SurfaceView prSurfaceView = (SurfaceView) findViewById(R.id.surface_camera);
SurfaceHolder prSurfaceHolder = prSurfaceView.getHolder();
prSurfaceHolder.addCallback(this);
prSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
Camera prCamera = Camera.open();
Camera.Parameters lParam = prCamera.getParameters();
prCamera.setParameters(lParam);
prCamera.setPreviewDisplay(prSurfaceHolder);
prCamera.startPreview();
prCamera.stopPreview();
1. Video Recording and Live Preview at Recording
The state transition diagram for Android MediaRecorder class is as below,
Figure 2. State Transition of Android MediaRecorder
The implementation follows the state transition diagram closely. Starting from Initial state, our code sets the audio source as microphone, video source as camera, and the program enters Initialized state. The output format can be set to mpeg4 or 3gp, which indicates the container format for the video recorded. At DataSourceConfigured state, we set the audio encoder as AMR_NB (Later versions of Android supports AAC), and video encoder as MPEG4,H263 or H264 based on user configurations. We also set the frame rate, and output file here. After that, we call prepare method to transit to Prepared state, and finally a start method call will start the recording.
The description above doesn’t contain every detail but just for illustration. The actual code in feipeng.yacamcorder.Main.java contains lots of error handling and code for remembering and setting user configurations.
Note that the encoders a device supports may differ.
2. Download
You can download the entire source code for the example above from here, or from my github repo.
References:
1. http://developer.android.com/guide/appendix/media-formats.html
2. http://developer.android.com/guide/topics/media/camera.html
9 comments on “Android Video Recording API–Illustrated with an Example”
Leave a Reply Cancel reply
40% Discount on My Book — Android NDK Cookbook
Android NDK Cookbook ebook 40% discount with promotion code MREANC40 at Packt Publishing The promotion code is valid until 15th June.Categories
- Android Apps (18)
- Android Audio Editor (1)
- TS 2 (3)
- Video Converter Android (8)
- Video2Gif (1)
- Android Tutorial (26)
- Android Dev Tools (1)
- API illustrated (8)
- Multimedia API (3)
- ffmpeg on Android (4)
- NDK (6)
- UI (5)
- Animation (1)
- Code Snippet (2)
- Coding Beyond Technique (18)
- a word, a world (4)
- Bug Rectified (4)
- Programming Habit (1)
- Software as a Career (1)
- Software as User Experience (1)
- Compilers and Related (2)
- ELF (2)
- Computer Languages (31)
- C/C++ (13)
- Java (9)
- JavaScript (2)
- PHP (1)
- Python (8)
- Data Structure & Algorithms (29)
- Bits (1)
- Data Structure (5)
- Integers (10)
- BigInteger (1)
- Prime (4)
- Search (3)
- Sorting (5)
- Strings (5)
- Database (1)
- SQLite (1)
- Digital Signal Processing (33)
- Distributed Systems (17)
- Apache Cassandra (6)
- Apache Hadoop (8)
- Apache Avro (3)
- Apache Nutch (3)
- Apache Solr (1)
- Linux Study Notes (40)
- crontab (1)
- Linux Kernel Programming (8)
- Linux Programming (12)
- IPC (2)
- Linux Network Programming (5)
- Linux Signals (2)
- Linux Shell Scripting (1)
- ssh (3)
- Machinery (30)
- misc (1)
- My Ideas (1)
- My Project (3)
- Mobile Caching (1)
- Selective Decoding (2)
- My Publication (1)
- My Readings (1)
- Networking (15)
- Program for Performance (8)
- Uncategorized (1)
- Virtual Machine (2)
- Web Dev (8)
- web components (3)
- Android Apps (18)
Recent Comments
Archives
- May 2013 (1)
- April 2013 (1)
- March 2013 (4)
- December 2012 (2)
- November 2012 (6)
- October 2012 (6)
- September 2012 (3)
- August 2012 (13)
- July 2012 (15)
- June 2012 (3)
- May 2012 (8)
- April 2012 (4)
- March 2012 (13)
- February 2012 (19)
- January 2012 (9)
- December 2011 (11)
- November 2011 (12)
- October 2011 (4)
- September 2011 (12)
- August 2011 (16)
- July 2011 (15)
- June 2011 (6)
- May 2011 (10)
- April 2011 (13)
- March 2011 (20)
- February 2011 (4)
- November 2010 (2)
- May 2010 (1)
- April 2010 (1)
- February 2010 (1)





hi,
please tell me how to send this video to server side and record in a file on server side.I found
ParcelFileDescriptor fromSocket to send video to server, but i don’t know how to receive this on server side in a file
Is it possible??? please inform me, this my email address umer_rock_12@hotmail.com
Hi,
Please please help me. i have found a method for android video camera recording to pc but i m stuck to create a file on server side.
this methods are
1) Mediarecorder
parcelFileDescriptor fromSocket
that perfect to record video on sd card but when i send it to socket it doesn’t receive on server side not create a video file.
Thanks and Regards,
Umer
I haven’t done that. You can find help from the source where you found the method.
Hello,,
could you please tell us how to send this video to the server(PC)by socket(UDP)
Thanks for you
Anas
Sorry, I cannot help you do programming. You’ll need to figure it out yourself. I’m pretty sure there’re resources available online to guide you.
Thanks for this awesome tutorial. works great for me.
hi…
can plz tell me how to encode video for android screen.
I downloaded the source of this tutorial, when I tried to run it I obtained the following Android Lint error:
Errors
call requires api level 8 (current minimum is 6:)android.hardware.Camera#reconnect from Min.java line 209
Could you please suggest a how to fix this error
Thanks for the tutorial
Cheers
JR
I shut the SDK down and started all over again, and that seemed to have fixed the problem.
However my system crashes anytime I attempt to record a video. I am using a Samsung galaxy PT-1000 tablet