Playing Video

Video is more than just a bunch of pictures shown one right after another. It's sound as well, and the sound has to be closely synchronized with the images.

Android's MediaPlayer class works with video the same way it does with plain audio. The only difference is that you need to create a Surface for the player to use to draw the images. You can use the start() and stop() methods to control playback.

I'm not going to show you another MediaPlayer example, however, because there is a simpler way to embed videos in your application: the VideoView class. To demonstrate it, create a new Android project called Video using these parameters:

Project name: Video Build Target: Android 2.2 Application name: Video Package name: org.example.video Create Activity: Video Min SDK Version: 8

Change the layout (res/layout/main.xml) to this:

Download Videov1/res/layout/main.xml

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

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width= "fill_parent" android:layout_height="fill_parent"> <VideoView android:id="@+id/video" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_gravity="center" /> </FrameLayout>

Open Video.java, and change the onCreate() method as follows:

Download Videov1/src/org/example/video/Video.java

package org.example.video;

import android.app.Activity; import android.os.Bundle; import android.widget.VideoView;

public class Video extends Activity { @Override public void onCreate(Bundle savedlnstanceState) { super.onCreate(savedlnstanceState);

// Fill view from resource setContentView(R.layout.main);

VideoView video = (VideoView) findViewByld(R.id.video);

// Load and start the movie video.setVideoPath("/data/samplevideo.3gp"); video.start();

The setVideoPath( ) method opens the file, sizes it to its container while preserving the aspect ratio, and begins playing it.

Now you need to upload something to play. To do that, run the following command:

C:\> adb push c:\code\samplevideo.3gp /data/samplevideo.3gp

1649 KB/s (369870 bytes in 0.219s)

You can find samplevideo.3gp in the download package for this book, or you can create one of your own. The directory used here (/data) is just for illustrative purposes and should not really be used for media files.

Figure 5.3: Embedding a video is easy with VideoView.

It will work only on the emulator because that directory is protected on real devices.

Note that Android doesn't seem to care what extension you give the file. You can also upload and download files in Eclipse with the File Explorer view in the Android perspective, but I find the command line to be easier for simple things like this.

There's one more thing: we'd like the video to take over the whole screen including the title bar and status bar. To do that, all you need to do is specify the right theme in AndroidManifest.xml:

Download Videov1/AndroidManifest.xml

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

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="org.example.video" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name">

<activity android:name= ".Video"

android:label ="@string/app_name"

android:theme="@android:style/Theme.NoTitleBar.Fullscreen"> <intent-filter>

<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application>

<uses-sdk android:minSdkVersion="3" android:targetSdkVersion="8" /> </manifest>

once all that is done, when you run the program, you should see and hear the movie clip (see Figure 5.3, on the preceding page). Try rotating the display to verify it works in both portrait and landscape modes. Voila! Instant video goodness.

Now let's polish up the Sudoku sample with a little mood music.

0 0

Post a comment