During recording, we will need to preview the recorded image. To do this, I insert the SurfaceView component in the layout file. He will do the mini-screen camera. I set the size and width of this preview to be small enough to fit on most Android phone screens (layout_width and layout_height). I do not give fill_parent, because I want next to put a button to start and stop recording.

xml version = “1.0” ¬†encoding = “utf-8” ?>

<LinearLayout xmlns: android = ” http://schemas.android.com/apk/res/android “

android: orientation = “vertical”

android: layout_width = “fill_parent”

android: layout_height = “fill_parent”

>

<LinearLayout

android: orientation = “horizontal”

android: layout_width = “fill_parent”

android: layout_height = “fill_parent”

>

<SurfaceView

android: id = “@ + id / videoview”

android: layout_width = “360px”

android: layout_height = “240px” />

<Button

android: id = “@ + id / mybutton”

android: layout_width = “wrap_content”

android: layout_height = “wrap_content”

android: text = “record”

/>

LinearLayout>

LinearLayout>

You also need to take care of the necessary permissions of the application. My camera will record not only the image but also the sound – hence the power to record audio. The recorded file itself will be stored on an external memory card – hence WRITE_EXTERNAL_STORAGE. If you intend to save a recorded video in the built-in memory, you do not need this permission.

<uses-permission android: name = “android.permission.CAMERA” /> <uses-permission android: name = “android.permission.RECORD_AUDIO” ¬†/> <-permissionandroid uses: name = “android.permission.WRITE_EXTERNAL_STORAGE” />

Now let’s move on to the code that will handle it all. In order for our activity to receive information that the preview surface has been prepared (and need to be prepared before recording begins), our activity must implement the SurfaceHolder.CallBack interface. Due to the implementation of this interface, we also have to implement the onSurfaceChanged method (but we will not develop it here). On line 20, I define the object that I will use to handle the button (hereafter, I will attach the reference to the button defined in the layout below it). On line 21 I create a MediaRecorder object. This is a class for recording video and sound. On line 22, I create a SurfaceHolder object. Using an object of this class we can eg configure the size and format of the recording,

 

On line 23, I create an object that will represent the preview screen of the recording. On the screen I will also have a button, initially will be labeled “Record”. When I press it for the first time, it is supposed to start recording and the inscription on it will change to “End”. When I click a second time, the recording and the file will be interrupted. So I need a variable (line 24), here the boolean type whose state will tell whether it is recording or not. The pathname of the path specified in line 25 is to indicate the name and path to the recording file that you want to create. Lines 27-43 is a method to handle button presses. The first time you click on a code from line 39-41, the recording starts and the text on the button changes to “End”. On the second click, code 28-38 is executed. Recording is interrupted, In addition, I check if a recording file has been created. If it is actually created, I will get this information from LogCata.

We are now going to the onCreate method, which is started at the start of the activity.

 

I am attaching references here to components that we will use, setting the recording only horizontally (line 51), I record the activity object as a receiver for recording events and preparing the preview screen (lines 54-56), and as a click of the button set the call to the previously discussed Click () (line 61). On line 52, I also call initMediaRecorder (), which looks like this:

 

It is used to set the sound and image source, picture quality, maximum recording time, and maximum file size.

At the start of the activity, a preview of the recording is also created. We have to prepare him for this occasion. In the surfaceCreated method (required for the implementation of the Callback interface), I call my own method prepareMediaRecorder, whose sole purpose is to do the above-mentioned action.