Logo Icon Logo
A Crowd-sourced Cookbook on Writing Great Android® Apps
Twitter logo OReilly Book Cover Art
HomeF.A.Q.
Community
Writing Recipes
Login
Playing a Video in your App
Chapter 10. Multimedia
Contributed by Ian Darwin 2012-04-17 10:42:22 (updated 2012-05-23 04:33:37)
In Published Edition? No
0
Votes
Problem

You want to play a Video in your application, as opposed to launching a media player application using an Intent (as shown in 1651).

Solution

The simplest way is to use a VideoView and call its setVideoURI method.

Discussion

You will typically create the layout in XML, so just add a VideoView element in the XML layout.

Then, in your onCreate() method, find the VideoView. Call its setVideoURI() method, and call its start() method. You are finished!

You can optionally pass in a MediaController if you wish the user to have the player controls (which they have to know to activate by tapping on the video pane). Use setMediaController; create a MediaController using the Activity in the constructor.

VideoView vw = (VideoView) findViewById(R.id.onboard_videoView);

vw.setVideoURI(Uri.parse(VIDEO_URL_STRING));
vw.setMediaController(new MediaController(this));
vw.start();

The VideoURI can be on the SD Card, in the res/raw or assets folder, or off-device if is is in a format that supports streaming. Make sure your video is in a supported format; see the official documentation for details on what video formats and CODECs are supported in various versions of Android.

That's all you have to do.

Comments (0)
Leave a comment
Edit History (4)
There are no (moderator-approved) comments on this recipe yet.