Playing video

Playing a video is slightly more complicated than playing audio with the MediaPlayer API, in part because you have to provide a view surface for your video to play on. Android has a VideoView widget that handles that task for you, and it can be used in any layout manager. Plus it provides a number of display options, including scaling and tinting. So let's get started with playing video by creating a new project called Simple Video Player. Then create a layout as shown in listing 10.3.

NOTE Currently the emulator has some issues playing video content on certain computers and operating systems. Do not be surprised if your audio or video playback is choppy.

Listing 10.3 main.xml—UI for Simple Video Player

<?xml version="1.0" encoding="utf-8 " ?>

<LinearLayout xmlns:android="" android:orientation="vertical" android:layout_width="fill_parent"


<VideoView android:id="@+id/video" android:layout_width="32 0px" android:layout_height="24 0px" />

<Button android:id="@+id/playvideo android:text="Play Video" android:layout_height="fill_parent android:layout_width="fill_parent" android:paddingRight="4px" android:enabled="false" />


All we have done in listing 10.3 is to add the VideoView widget O and a Button to initiate playback of our video •.

Next we need to write a class to play the video. In addition to the VideoView, we put in a Button that, when pushed, will pop up the VideoView control panel, known as the MediaController. This, by default, overlays the bottom portion of the VideoView and shows your current position in the video clip. Plus it offers pause, rewind, and fast-forward buttons. See listing 10.4.

B Add VideoView <_I widget

Listing 10.4 public class SimpleVideo extends Activity {

private VideoView myVideo; private MediaController mc;

@Override public void onCreate(Bundle icicle) { O

super.onCreate(icicle) ;

getWindow() . setFormat (PixelFormat .TRANSLUCENT) ; <1-'


Button bPlayVideo=(Button)findViewById(;

bPlayVideo.setOnClickListener(new View.OnClickListener() public void onClick(View view) {

}) ; Show the MediaController UI widget Q

Create a translucent window

Set the view and button to play MP4

this.myVideo=(VideoView)findViewByld(; this.myVideo.setVideoPath("sdcard/test.mp4"); MediaController(this);; this.myVideo.setMediaController(mc); this.myVideo.requestFocus();

Callback to VideoView when video is done

Pass file from sdcard to VideoView F

Use VideoView as container for playing video O

Create a MediaController

In listing 10.4 we first created a translucent window which is necessary for our Sur-faceView O. Then we added a Button to the VideoView widget Q and told Android to add a MediaController widget over the VideoView G using the show() method. Next we reference the VideoView O and use its setVideoPath() © to have it look at an SD card (sdcard) for our test MP4. Finally we set up the MediaController G and use the setMediaController() O to perform a callback to the VideoView to notify it when a our video is finished playing.

Before we can run this application, we need to set up an sdcard in the emulator (see chapter 5 for details on the SD card). First, create a new SD card image:

mksdcard 512M mysdcard

Hit Return. A 512 MB FAT32 image named mys-dcard has now been created for you to load into the emulator. Load the SD card into the emulator like this:

emulator -sdcard mysdcard

Now push the file test.mp4 to the disk image.

Once you have pushed the file to the image, you can launch the SimpleVideo application by going to your IDE and running the project while the emulator is already running. You should now see something like figure 10.3.

As you can see, the VideoView and MediaPlayer classes simplify working with video files Something you will need to pay attention to when working with video files is that the emulator often has problems with files larger than 1 megabyte, although the current G1 phone does not.

NOTE By default, G1 supports only MP4 and 3GP formats. There are several video convert ers you can use to convert videos in other formats to these standards. As Android adoption grows, you can expect updates and more players to support a greater number of formats.

Now that you have seen how simple it is to play media using Android's MediaPlayer API, let's look at how we can use a phone's built-in camera or microphone to capture images or audio.

0 0

Post a comment