Playback Using an Intent


Build your own Android App Dev Empire

Get Instant Access

As with most of Android's capabilities that we have explored in this book, simply playing back a video can be done easily, using an intent to trigger the built-in Media Player application's playback activity.

For this example, I captured a video using QuickTime X on a Mac laptop with a built-in iSight. I exported this video using QuickTime X's Save As command and selected "iPhone" as the format. This created a video that I named Test_Movie.m4v. (The .m4v extension was given by QuickTime. Unfortunately, it isn't standard, which may indicate that the file may not be a fully standards-compliant MPEG-4 file. Nevertheless, it doesn't seem to present any issues on Android devices.) This video is available online at for you to download if you would like to test with it.

The following code requires that this video be on root of the SD card on your Android device. To do this, you should be able to connect the device to your computer via a USB cable and select the menu item in the status bar pull-down that states "USB connected. Select to copy files to/from your computer." This should bring up a screen that allows you to "Turn on USB storage." Doing so should cause your phone to be mounted as a removable drive to your computer, and the video file can then be copied over. Don't forget to unmount the drive and "Turn off USB storage" before attempting to run the following code. If you don't, you'll get an error as the program running on the phone cannot access the SD card to read the video file while your computer has access to it.

To create the intent that will trigger the built-in Media Player application's playback activity, we'll construct an activity using the Intent.ACTION_VIEW constant and pass in the URI and the MIME-type of the file via the setDataAndType method. This allows Android to choose the preferred activity for playback. Figure 9-1 shows the built-in Media Player from Android 2.2 playing video specified in this manner.

package com.apress.proandroidmedia.ch09.videointent;

import; import android.content.Intent; import; import android.os.Bundle; import android.os.Environment;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

public class VideoPlayerIntent extends Activity implements OnClickListener { Button playButton;

@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);

playButton = (Button) this.findViewById(; playButton.setOnClickListener(this);

public void onClick(View v) {

Intent intent = new Intent(android.content.Intent.ACTION_VIEW); Uri data = Uri.parse(Environment.getExternalStorageDirectory().getPath() "/Test_Movie.m4v");

intent.setDataAndType(data, "video/mp4"); startActivity(intent);

Onclicklistener Video
Figure 9-1. Built-in Media Player application playing video specified via an intent

Was this article helpful?

0 0

Post a comment