The following is the first few sections of a chapter from The Busy Coder's Guide to Android Development, plus headings for the remaining major sections, to give you an idea about the content of the chapter.


Video Playback

Just as Android supports audio playback, it also supports video playback of local and streaming content. Unlike audio playback – which supports a mix of high-level and low-level APIs – video playback offers a purely high-level interface, in the form of the same MediaPlayer class you used for audio playback. To keep things a bit simpler, though, Android does offer a VideoView widget you can drop in an activity or fragment to play back video.

Prerequisites

Understanding this chapter requires that you have read the core chapters of this book, along with the chapter on audio playback.

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!

For example, take a look at this layout, from the Media/Video sample project:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
  <VideoView 
     android:id="@+id/video" 
      android:layout_width="match_parent"
      android:layout_height="match_parent"
    />
</LinearLayout>

The layout is simply a full-screen video player. Whether it will use the full screen will be dependent on the video clip, its aspect ratio, and whether you have the device (or emulator) in portrait or landscape mode.

Wiring up the Java is almost as simple:

package com.commonsware.android.video;

import java.io.File;
import android.Manifest;
import android.app.Activity;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.os.Environment;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;

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

  @Override
  protected String[] getDesiredPermissions() {
    return(new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE});
  }

  @Override
  protected void onPermissionDenied() {
    Toast
      .makeText(this, R.string.msg_sorry, Toast.LENGTH_LONG)
      .show();
    finish();
  }

  @Override
  public void onReady(Bundle state) {
    getWindow().setFormat(PixelFormat.TRANSLUCENT);
    setContentView(R.layout.main);
  
    File clip=new File(Environment.getExternalStorageDirectory(),
                       "test.mp4");
    
    if (clip.exists()) {
      video=(VideoView)findViewById(R.id.video);
      video.setVideoPath(clip.getAbsolutePath());
      
      ctlr=new MediaController(this);
      ctlr.setMediaPlayer(video);
      video.setMediaController(ctlr);
      video.requestFocus();
      video.start();
    }
  }
}

We use the AbstractPermissionActivity profiled earlier in the book, as we need READ_EXTERNAL_STORAGE rights to be able to read a test.mp4 video from the root of external storage.

Beyond that, we:

  1. Confirm that our video file exists on external storage
  2. Tell the VideoView which file to play
  3. Create a MediaController pop-up panel and cross-connect it to the VideoView
  4. Give the VideoView the focus and start playback

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, preferably on an SD card.

The crude VideoDemo class assumes there is an MP4 file named test.mp4 in the root of external storage on your device or emulator. Once there, the Java code shown above will give you a working video player:

VideoDemo, Showing a Creative Commons-Licensed Video
Figure 803: VideoDemo, Showing a Creative Commons-Licensed Video

Tapping on the video will pop up the playback controls:

VideoDemo, Showing Media Controls
Figure 804: VideoDemo, Showing Media Controls

The video will scale based on space as well:

VideoDemo, in Landscape Mode