Course Content

Course Content

Description

Surface View and Media Recorder

 

In today's video our topic is Surface View and Media Recorder.

Surface view represents preview of live camera. It is used when we want any live surface preview in our application in order to take image or video.

Here is one example of SurfaceView,

public class CameraView extends SurfaceView implements SurfaceHolder.Callback

{

private SurfaceHolder mHolder;

private Camera mCamera;

public CameraView(Context context,Camera camera)

{

super(context);

mCamera=camera;

mCamera.setDisplayOrientation(90);

mHolder=getHolder();

mHolder.addCallback(this);

mHolder.setType(SurfaceHolder.SURFACE_TYPE_NORMAL);

}

Now suppose in some case, we need to capture the image in landscape mode or change the mode of orientation. So before changing the application orientation, you need to stop the preview, rotate and then start it again.

Important part of code for that is :

if(mHolder.getSurface()==null)

return;

try

{

mCamera.stopPreview();

}

catch(Exception e)

{

}

try

{

mCamera.setPreviewDisplay(mHolder);

mCamera.startPreview();

}

catch(IOException e)

{

Log.d("ERROR","Camera error on surfaceChanged"+e.getMessage());

}

}

@Override

public void surfaceDestroyed(SurfaceHolder surfaceHolder)

{

mCamera.stopPreview();

mCamera.release();

}

Now we use Media Recorder when we want to capture video in android application. There are several methods of camera application which we need to use carefully while media recording, they are, Camera lock, Camera unlock, Camera open, Camera.release.

We need to follow the below mentioned specific order for MediaRecorder,

Unlock camera with Camera.unlock()

Configure MediaRecorder

Start recording using MediaRecorder.start()

Record the video

Stop recording using MediaRecorder.stop()

Release media recorder with MediaRecorder.release()

Lock camera using Camera.lock()

FAQs

Recommended Courses

Share With Friend

Have a friend to whom you would want to share this course?

Download The Apps

Code Scan or Download the app
Google Play Store
297K+ Downloads
4.5 Rating
10K+ Reviews
  • Learn anywhere on the go
  • Get regular updates about your enrolled or new courses
  • Share content with your friends
  • Evaluate your progress through practice tests
  • No internet connection needed
  • Enroll for the webinar and join at the time of the webinar from anywhere