Previewing Video Recording

When recording video, it's generally consideredgood practice to display a preview of the incoming video feed in real time. Using the setPreviewDisplay method, you can assign a Surface to display the video stream in real-time.

This works in much the same way as described earlier in this chapter when playing video using the Media Player.

Start by creating a new Activity that includes a SurfaceView control as part of the UI, and which implements the SurfaceHolder.Callback interface.

Once the Surface Holder has been created, assign it to the Media Recorder using the setPreviewDisplay method as shown in Listing 11-10.

The live video preview stream will begin displaying as soon as you make a call to prepare.

LISTING 11-10: Previewing video recording Available for download on public class MyActivity extends Activity implements SurfaceHolder.Callback Wrax.com {

private MediaRecorder mediaRecorder;

©Override public void onCreate(Bundle savedlnstanceState) { super.onCreate(savedlnstanceState); setContentView(R.layout.main);

SurfaceView surface = (SurfaceView)findViewByld(R.id.surface); SurfaceHolder holder = surface.getHolder(); holder.addCallback(this);

holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); holder.setFixedSize(400, 300);

public void surfaceCreated(SurfaceHolder holder) { if (mediaRecorder == null) { try {

mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);

mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);

mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT); mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT); mediaRecorder.setOutputFile("/sdcard/myoutputfile.mp4");

mediaRecorder.setPreviewDisplay(holder.getSurface()); mediaRecorder.prepare();

} catch (IllegalArgumentException e) {

Log.d("MEDIA_PLAYER", e.getMessage()); } catch (IllegalStateException e) {

Log.d("MEDIA_PLAYER", e.getMessage()); } catch (IOException e) {

Log.d("MEDIA_PLAYER", e.getMessage());

public void surfaceDestroyed(SurfaceHolder holder) { mediaRecorder.release();

public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { }

USING THE CAMERA AND TAKING PICTURES

The popularity of digital cameras (particularly within phone handsets) has caused their prices to drop just as their size has shrunk dramatically. It's now becoming difficult to even find a mobile phone without a camera, and Android devices are certainly no exception.

The G1 was released in 2008 with a 3.2-megapixel camera. Today several devices feature 5-megapixel cameras, with one model sporting an 8.1-megapixel sensor.

The following sections will demonstrate the mechanisms you can use to control the camera and take photos within your applications.

Mobile Apps Made Easy

Mobile Apps Made Easy

Quick start guide to skyrocket your offline and online business success with mobile apps. If you know anything about mobile devices, you’ve probably heard that famous phrase coined by one of the mobile device’s most prolific creators proclaiming that there’s an app for pretty much everything.

Get My Free Training Guide


Post a comment