Play Video using VideoView in ANDROID.

By | September 19, 2010

Playing a video file is simple in android. This post will help you.
Put your video in res/raw folder. If no raw folder present then create a new folder inside res.

package com.coderzheaven.pack;

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

public class Example extends Activity {
    /** Called when the activity is first created. */
	public void onCreate(Bundle savedInstanceState)
        {
           super.onCreate(savedInstanceState);
           setContentView(R.layout.main);
           showVideo();
       }
	private void showVideo()
	{
		VideoView vd = (VideoView)findViewById(R.id.videoview);
		Uri uri = Uri.parse("android.resource://package/"+R.raw.movie);
		MediaController mc = new MediaController(this);
		vd.setMediaController(mc);
		vd.setVideoURI(uri);
		vd.start();
	}
}

The main. xml file.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/widget32"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
	<VideoView
			android:id="@+id/videoview"
			android:layout_width="fill_parent"
			android:layout_height="fill_parent"
			android:layout_alignParentTop="true"
			>
		</VideoView>
</RelativeLayout>

Please leave your comments if this post was useful.

15 thoughts on “Play Video using VideoView in ANDROID.

  1. unknown

    hi this code is really good but when i put any fil e on raw folder the invalid file name error occur pls suggest me to solve this problem

    Reply
      1. Dale

        Hello James, thank you for reply. Can you help me once more? When I run a video and sliding on page during ten seconds, I get pop up menu ‘Activity MyActivity (in application MyActivity) is not responding’ appears (Android 3.2.1). How to ban the sliding?

        Reply
        1. James

          @Dale :- I couldn’t understand clearly what you are trying to say.

          Reply
          1. Dale

            When I try paging on screen during 10 sec., I get error ‘Activity MyActivity (in application MyActivity) is not responding’. This error shows on Android 3.2.1 (on Android 2.3.3 – all OK). How to freeze the paging on screen?

  2. Pingback: How to check whether a video has completed playing in android? | Coderz Heaven

  3. nikunj

    Hello Sir,
    When i run your code so we can only listening music video is not see in my emulator…….
    please help me sir

    Reply
  4. aunty

    Hello James,

    How to play Video in full screen mode?

    I have used this for videoview in xml

    android:layout_alignParentRight=”true”
    android:layout_alignParentLeft=”true”
    android:layout_alignParentTop=”true”
    android:layout_alignParentBottom=”true”

    but it doesnt display it in full screen.

    Thanks in advance.

    Reply
  5. Ankush

    Hey,
    When i put this code i my application it shows a pop up saying video can not be played.
    Plz help

    Reply
    1. James

      Check whether you video is in supported format or try your project on the device.

      Reply
  6. Pingback: how to display the videos in listview in youtube player? : Android Community - For Application Development

Leave a Reply to dfejrwh Cancel reply

Your email address will not be published. Required fields are marked *