Playing video in an Android application


          Android's MediaPlayer class works with video files the same way it does with plain audio/music files. But there is a small difference. We need to create a surface to play the video. We can use start() or stop() methods to control the play.
We have already seen the use of MediaPlayer in audio example here. So to play the video we are going with different option, the VideoView class.
This is the simplest way of playing a video file. I have placed a sample 3gp video file inside /res/raw directory. We need to <VideoView> in the layout xml file and use it in the activity class.
VideoView class provides bunch of methods that can be used in video play.

setVideoPath() method is used to specify the path of the video file to be played. This can either be a local path or a remote URL.
setVideoURI() method is same as above method except the parameter. This taker URI as parameter.
start() method is used to start the video playback.
stop() method is used to stop the video playback.
pause() method pauses the video.
To check whether the video is playing or not isPlaying() method can be used. This will return a boolean value.
getCurrentPosition() method will return the current position of the video playback.

Sample application:
In this sample application we have a video file inside /res/raw directory and we are going to play this using VideoView class.

Project structure:

To use the full screen view we have used FullScreen theme in this application. This should be specified in the manifest xml file.


To get the URI of the video file placed inside the /res/raw directory below statement is used;

android.resource://" + getPackageName() + "/" +

AndroidManifest.xml file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=""
android:versionName="1.0" >

android:targetSdkVersion="16" />

android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >
android:label="@string/app_name" >
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />


Layout xml file:

<RelativeLayout xmlns:android=""
tools:context=".MainActivity" >

android:layout_gravity="center" />


Activity class:

package com.example.playvideo;

import android.os.Bundle;
import android.view.Menu;
import android.widget.VideoView;

public class MainActivity extends Activity {

protected void onCreate(Bundle savedInstanceState) {
VideoView video = (VideoView) findViewById(;
// Load and start the movie
Uri video1 = Uri.parse("android.resource://" + getPackageName() + "/" +;

public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(, menu);
return true;

Output screenshot:

Source code of this application:


1 comment:
Write comments
  1. Bro Pls Tell me what is the fonction of this methode :/ @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(, menu);
    return true;


Popular Posts