Video playback from the SD Card

Playing back video from the SD card requires little code, but it does require some prep work with regard to creating and configuring the emulator with the SD card. By default, the Android 1.1 emulator is not enabled with an SD card, so you first need to create an SD-card image and configure the emulator to use the SD card. After the card is created and the emulator is made aware of it, you can then use the Android tools within Eclipse to push the video file (or any other file) to the SD card. After the file has been loaded onto the card, you can run the video sample to play the video file at /sdcard/movie.mp4, for example. To configure an SD card for the Android 1.5 emulator, see Chapter 12.

Now let's create the SD-card image. The Android tools bundle contains a utility called mksdcard that can create an SD-card image. Actually, the utility creates a formatted file that is used as an SD card. To use this utility, first create a folder for the image file at c:\Android\ sdcard\, for example. Then open a command line to the Android SDK /tools directory and run the following command:

mksdcard 256M c:\Android\sdcard\sdcard.img

The command creates an SD-card image at c:\Android\sdcard\ with a file name of sdcard.img. The size of the SD card will be 256MB. After you create the SD-card image, you need to point the emulator to the image. You do this by passing a command-line argument to the emulator, as shown in Figure 9-3.

Figure 9-3. Passing command-line arguments to the emulator

In Eclipse, open the release/debug configuration that you want to run (select Run > Run Configurations...). Select the "Android Application" node from the left pane and choose a project to show its release/debug configuration in the right pane. Select the Target tab and set the "Additional Emulator Command Line Options" field to

-sdcard c:\Android\sdcard\sdcard.img

Note that the value of the -sdcard argument is the physical path to the SD-card image on your workstation. Before you can run the video sample from the SD card, you need to upload a video file to the SD card. You can do that by using the File Explorer tool. Start the emulator after you set the - sdcard command-line option and wait until the emulator initializes. Then open Eclipse in Debug perspective. You should see the File Explorer view as shown in Figure 9-4.

Figure 9-4. The File Explorer view

If the File Explorer is not shown, you can bring it up by going to Window > Show View > Other > Android. Alternatively, you can show the Dalvik Debug Monitor Service (DDMS) perspective by going to Window > Open Perspective > Other > DDMS, which will show all of the views shown in Figure 9-5.

Figure 9-5. Enabling Android views

To push a file onto the SD card, select the sdcard folder in the File Explorer and choose the button with the right-facing arrow (at the top-right corner). This launches a dialog box that lets you select a file. Select the movie file that you want to upload to the SD card. In our example, we assume that the file is located at the root of the SD card and that it's named movie.mp4 (see Listing 9-3). After the file is uploaded, you should be able to run the video sample in Listing 9-3 using the SD-card video file. The only change you'll need to make to Listing 9-3 is to update the setVideoURI() method with a file URL rather than an HTTP URL:

videoView.setVideoURI(Uri.parse("file:///sdcard/movie.mp4"));

Realize that if the File Explorer displays an empty view, you either don't have the emulator running or the project that you are running in the emulator is not selected under the Devices tab shown in Figure 9-4.

Finally, note that there are two ways to feed video content to VideoView: setVideoPath() or setVideoURI(). setVideoPath() takes a physical path, whereas setVideoURI() accepts a URL.

0 0

Post a comment