Logo Icon Logo
A Crowd-sourced Cookbook on Writing Great Android® Apps
GitHub logo Twitter logo OReilly Book Cover Art

Playing a Video in your App

Published? false
FormatLanguage: WikiFormat

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.