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="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    
   <VideoView 
       android:id="@+id/surface_view" 
       android:layout_width="320px"
       android:layout_height="240px"/>
</LinearLayout>

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.java. VideoViewExample class will look like following:

package com.sample.VideoViewExample;
import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.widget.MediaController;
import android.widget.VideoView;

public class VideoViewExample extends Activity {
   private VideoView mVideoView;

   @Override
   public void onCreate(Bundle icicle) {
     super.onCreate(icicle);
     setContentView(R.layout.main);
     mVideoView = (VideoView) findViewById(R.id.surface_view);
     mVideoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() +"/"+R.raw.documentariesandyou));
     mVideoView.setMediaController(new MediaController(this));
     mVideoView.requestFocus();
   }
}

Output –The final output:

About the Author

Leave a Reply 25 comments

Dharmesh Garg - June 15, 2011 Reply

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

Osama - January 8, 2012 Reply

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

vineet - February 11, 2012 Reply

Hi,
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.

Sudhir - February 23, 2012 Reply

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

rams - May 17, 2012 Reply

hi..

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.

krishnakiran94 - June 11, 2012 Reply

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

Dewa - August 27, 2012 Reply

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?

Invictus - August 28, 2012 Reply

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!

Girish - September 24, 2012 Reply

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

preeti jain - October 13, 2012 Reply

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

preeti jain - October 13, 2012 Reply

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.

juhi - December 3, 2012 Reply

what is the meaning of getpakagename()

Preethi - December 19, 2012 Reply

it works awesome

Raj - January 30, 2013 Reply

Hi,

Can we play the video in background?

Thanks,
Raj

Aditya rashiya - February 4, 2013 Reply

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.

Umesh - April 1, 2013 Reply

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

Juan - April 1, 2013 Reply

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

sanal varghese - April 7, 2013 Reply

Hi,

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……:)

Alver - May 3, 2013 Reply

Awesome ^^

toantoet - May 10, 2013 Reply

How can I create mediaplaye as youtube player?

takitta - May 15, 2013 Reply

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:

package com.example.video;

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.widget.MediaController;
import android.widget.VideoView;

public class MainActivity extends Activity {
VideoView vid;

@Override
public void onCreate(Bundle savedIstanceState){
super.onCreate(savedIstanceState);
setContentView(R.layout.activity_main);
vid = (VideoView) findViewById(R.id.vid);
String urlpath = “android.resource://” +getPackageName() + “/” + R.raw.video_test;
vid.setVideoURI(Uri.parse(urlpath));
vid.setMediaController(new MediaController(this));
vid.start();
vid.requestFocus();
}
}

Code .XML :

Sanjay - May 16, 2013 Reply

Very very helpfully example…………tnx…

aldrich_yan - August 1, 2013 Reply

if i can place a button on the vedio ?

aldrich_yan - August 1, 2013 Reply

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

Leave a Reply: