Setting up a Surface for Video Playback

The first step to using the Media Player to view video content is to prepare a Surface onto which the video will be displayed. The Media Player requires a SurfaceHolder object for displaying video content, assigned using the setDisplay method.

If you do not assign a Surface Holder for your Media Player the video component will not be shown.

To include a Surface Holder in your UI layout you use the SurfaceView control as shown in the sample layout XML in Listing 11-4.

Available for download on

LISTING 11-4: Sample layout including a Surface View Available for download on <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <SurfaceView android:id="@+id/surface" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center"> </SurfaceView> </LinearLayout>

The Surface View is a wrapper around the Surface Holder object, which in turn is a wrapper around the Surface that is used to support visual updates from background threads.

The Surface View will be examined in more detail in Chapter 15, but Listing 11-5 shows the skeleton code used to initialize a Surface View within your Activity, and assign it as a display target for your Media Player.

Note that you must implement the SurfaceHolder.Callback interface. Surface Holders are created asynchronously, so you must wait until the surfaceCreated handler has been fired before assigning the returned Surface Holder object to the Media Player.

LISTING 11-5: Initializing and assigning a Surface View to a Media Player Available for download on public class MyActivity extends Activity implements SurfaceHolder.Callback {

private MediaPlayer mediaPlayer; ©Override public void onCreate(Bundle savedlnstanceState) { super.onCreate(savedlnstanceState); setContentView(R.layout.main); mediaPlayer = new MediaPlayer();

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

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

public void surfaceCreated(SurfaceHolder holder) { try {


} 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) { mediaPlayer.release();

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

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