« Clip-on ties and clip-on webcams | Main | Mobile Youtube Problems »

Video Playback in the Android SDK

Lots of developers want to know how to do this. Here's how. Note that this assumes you are using the Android plug-in for eclipse.

First, you need to create your own sdcard image file for use by the emulator. The tool to do this is called mksdcard and is located in \android-sdk-windows-1.0_r2\tools. Open a command window and go to this the tools directory.

The command is very simple:
mksdcard 100M mycard.iso

100M signifies a 100 MB card (and capital M is required). 'mycard.iso' is the file name for the card image. To load files on your new image, you can use the adb tool. It has many uses (displaying debugging output and access to the android emulator linux command line, to name a couple).

Here's how to copy files:
First, the emulator must be running. You can start from eclipse or from the command line.
Second, run something like this:

adb push myvideo.mp4 /sdcard

'myvideo.mp4' is the file you want to send to the card. In this example, the file is already in the tools directory, but you could specify a path to a file located elsewhere (I presume). The card image keeps the files you 'push' to it. That is, they are persistent.

To launch the emulator and have it use your new image, do this:
emulator -sdcard C:\android-sdk-windows-1.0_r2\android-sdk-windows-1.0_r2\tools\mycard.iso

You should put this in the eclipse run configuration for android- but you must use an absolute file path to the sdcard image file.

A note on video. So far, I have played 3gp files, mp4 files, and mp4 files with h.264 content using this technique. I use AAC audio in the video files. You can also play video files hosted on an http server, as long as they are of the types mentioned above. It is also important to note that you need to use the domain name or IP address of the web server- using 'localhost' or '127.0.0.1' with your apache server on your dev box won't work. By the way, this is not true streaming but simply progressive download. The files download over http and the player starts playing the bits as they  come in. If you set the emulator to use a slow network, the video can start/stop while buffering frames for playback. I also found that video files at higher than 1 mbps (1.3 or so) failed to play. The only friendly error message I have seen regarding video happens during high data-rate playback:

D/dalvikvm(  106): GC freed 2362 objects / 130584 bytes in 154ms
W/PlayerDriver(   24): Video cannot catch up. Are you playing a high quality content?
E/MediaPlayer(  194): Error (44,0)
D/VideoView(  194): Error: 44,0

You will also find that video, even when downloaded, is not cached. That is up to you.

I have played mp3 music files by including them as a 'raw' resource as part of my android test application. I have not been able to play video this way, though.

Here's the basic java code to run (just change the url to your video):

        setContentView(R.layout.videoview);
        controller = new MediaController(this);
        viewer = (VideoView)findViewById(R.id.layout_viewer);
        viewer.setMediaController(controller);
        viewer.requestFocus();
       

Call this code from a separate method activated by a button or menu (why? It won't play when all in the same method).       

        viewer.setVideoURI(Uri.parse("http://172.20.18.78:8080/venus-h264.mp4"));
        viewer.start();

Here's a layout element:

<VideoView android:id="@+id/layout_viewer"
        android:layout_width="fill_parent"
    android:layout_height="fill_parent"    />

 

 

TrackBack

TrackBack URL for this entry:
http://www.erichizdepski.com/blog-mt1/mt-tb.fcgi/58


Hosting by Yahoo!

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)