Moving Pictures

Video clips get their own widget, the videoview. Put it in a layout, feed it an MP4 video clip, and you get playback!

Right now, playback seems a bit rocky in the emulator, but that will likely clear itself up in future releases - videoview was only made available in the SDK release prior to publication of this book.

Since videoview is a widget, you can put it in a layout, such as this one from the VideoDemo sample project:

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

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

android:layout_height="fill_parent" >

<videoview android:id="@+id/video"

android:layout_width="320px" android:layout_height="240px"

<Button android:id="@+id/show" android:text="Show Controller!" android:layout_height="fill_parent" android:layout_width="fill_parent" android:paddingRight="4px" android:enabled="false"


In addition to the videoview, we also 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 offers pause, rewind, and fast-forward buttons:




import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.MediaController;

import android.widget.VideoView;

public class VideoDemo extends Activity { private VideoView video; private MediaController ctlr;

(Override public void onCreate(Bundle icicle) { super.onCreate(icicle);

getWindow().setFormat(PixelFormat.TRANSLUCENT); setContentView(R.layout.main);

Button show=(Button)findViewById(;

show.setOnClickListener(new View.OnClickListener() {

public void onClick(View view) {;

video=(VideoView)findViewById(; video.setVideoPath("/tmp/test.mp4");

ctlr=new MediaController(this); ctlr.setMediaPlayer(video);

video.setMediaController(ctlr); video.requestFocus();

The biggest trick with videoview is getting a video clip onto the device. While videoview does support some streaming video, the requirements on the MP4 file are fairly stringent. If you want to be able to play a wider array of video clips, you need to have them on the device, either in the local filesystem or on an SD card.

The crude videoDemo class assumes there is an MP4 file in /tmp/test.mp4 on your emulator. To make this a reality:

1. Find a clip, such as Aaron Rosenberg's Documentaries and You from Duke University's Center for the Study of the Public Domain's Moving Image Contest, which was used in the creation of this book

2. Use the adb push command (or the equivalent in your IDE) to copy the MP4 file into /tmp/test.mp4

Once there, the following Java code will give you a working video player:

Show Controller!

Figure 65. The VideoDemo sample application, showing a Creative Commons-

licensed video clip

NOTE: the /tmp directory is cleaned out periodically on the emulator, and so you may need to re-push the file if you intend to run this sample over an extended period of time.

The button is set up to call show() on the MediaController, which displays the control panel. The clip will automatically start playing back - you do not need to call play() on the videoview, though that method is available (as is pause() and stopPlayback(), in case you need your own control over playback in addition to the MediaController's control panel).

Video Marketing Gold

Video Marketing Gold

Do you already know the huge impact that video could have on your online business BUT have no idea where to begin with it? Discover Exactly How You Can Start Taking Advantage of Video Marketing In Your Online Business... Even If You're a Total Newbie... Starting Today.

Get My Free Ebook

Post a comment