How to play a Video file


This is a sample activity which shows How to run a video file.

Underlying Algorithm:

Basic description of algorithm in step by step form:
1.) Create a Project VideoViewExample.
2.) Put a video file in raw folder and keep it inside the res folder.
3.) Put the following code snippet in main.xml:

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

4.) Run the application.

Steps to Create:

1.) Open Eclipse. Use the New Project Wizard and select Android Project Give the respective project name i.e. VideoViewExample. Enter following information:
Project name: VideoViewExample
Build Target: Google APIs
Application name: VideoViewExample
Package name: com.sample.VideoViewExample
Create Activity: VideoViewExample

On Clicking Finish VideoViewExample code structure is generated with the necessary Android Packages being imported along with VideoViewExample class will look like following:

package com.sample.VideoViewExample;
import android.os.Bundle;
import android.widget.MediaController;
import android.widget.VideoView;

public class VideoViewExample extends Activity {
   private VideoView mVideoView;

   public void onCreate(Bundle icicle) {
     mVideoView = (VideoView) findViewById(;
     mVideoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() +"/"+R.raw.documentariesandyou));
     mVideoView.setMediaController(new MediaController(this));

Output –The final output:

Did you enjoy this article?
Get Free Updates

25 Responses

  1. Dharmesh Garg says:

    what are the supported file formats which can be played on android emulator

  2. Sushant says:

    It supports H.263 encoding and decoding, H.264 AVC and MPEG-4 SP both only decoding.

    Checkout the chart of all supported media formats for more information.

  3. Osama says:

    I tried the above code but i got a problem there is no video display just the audio sound

  4. vineet says:

    I have a two problem.
    1)when mp3 files playing then how split the particular part of the song in android.
    2)How fetch the images that is associated with songs.

    please help me.

  5. Sudhir says:

    After requestFocus, you should do mVideoView.start() to actually start playing the video.

  6. rams says:


    i done this project as same as that of u.but video did not get visible and mediacontroller got visible.pls send my error let me know.

  7. krishnakiran94 says:

    actually video playing but cannt see in emulator after sumtime it is showing how can i see immediately by clicking play button? help me out

  8. Dewa says:

    thanks for the tutorial. i try in emulator but the only thing that shown just the voice, but not the video. Can you point me the error?

  9. Invictus says:

    no the video is shown alright..but eclipse emulator really sucks. if you really want a HD experience, try using an actual device. however, use the standard virtual device you always use. using others might not always work. anyways, a very ice piece of work. i am new to android development..and videoview really made it simple. trying to design a player from scratch can be quite taxing. pheeww!

  10. Girish says:

    Hi, I want to know , can we play video in a browser or can we make video conferencing in android ?

  11. preeti jain says:

    can i play the mp4 format video in the emulator through raw folder

  12. preeti jain says:

    sir when i try to play the video from the raw folder.the logcat show the following error:
    unable to create media player
    could not open file on client side ,trying server side.

  13. juhi says:

    what is the meaning of getpakagename()

  14. Preethi says:

    it works awesome

  15. Raj says:


    Can we play the video in background?


  16. nice tutorial i want add one functionality that after video play of 5 sec. appear one alert dialog like “u want continue play? n two button like yes or no if user click on yes then video play continue n click on no suddenly video stop n going to previouse activity.

  17. Umesh says:

    @Juhi : it gives the package name of current class file

  18. Juan says:

    Great example, i did it with 3gp file format and had no problems, thanks

  19. sanal varghese says:


    i tried this sample.It works all version except Android 2.2. It’s not playing Android 2.2. When i googled, many reported same issue. but i cannot find a alternative solution. Can you suggest me an alternative if you have any. Thanks for the great work and for sharing you knowledge with world. Keep going……:)

  20. Alver says:

    Awesome ^^

  21. toantoet says:

    How can I create mediaplaye as youtube player?

  22. takitta says:

    I have always a msg ” can’t play this video” .. i don’t understand the error !! thnakyou to help me as soon as possible

    JAVA code:


    import android.os.Bundle;
    import android.widget.MediaController;
    import android.widget.VideoView;

    public class MainActivity extends Activity {
    VideoView vid;

    public void onCreate(Bundle savedIstanceState){
    vid = (VideoView) findViewById(;
    String urlpath = “android.resource://” +getPackageName() + “/” + R.raw.video_test;
    vid.setMediaController(new MediaController(this));

    Code .XML :

  23. Sanjay says:

    Very very helpfully example…………tnx…

  24. aldrich_yan says:

    if i can place a button on the vedio ?

  25. aldrich_yan says:

    I mean if i can show ui when play the vedio?thanks

Leave a Reply

two × 4 =

Proudly designed by Theme Junkie.