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 Comments

  1. dfejrwh January 5, 2011 at 10:40 pm

    useful code.. thanks!!!

     
  2. unknown November 30, 2011 at 3:31 pm

    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

     
    • James November 30, 2011 at 9:23 pm

      Did you use your package name in the place of “package”.

       
  3. Dale January 6, 2012 at 3:33 pm

    How to programatically close the player when the video ended.

     
    • James January 6, 2012 at 9:18 pm
       
      • Dale January 8, 2012 at 4:05 pm

        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?

         
        • James January 8, 2012 at 9:05 pm

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

           
          • Dale January 8, 2012 at 10:03 pm

            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?

             
          • Dale January 13, 2012 at 4:22 pm
             
  4. Pingback: How to check whether a video has completed playing in android? | Coderz Heaven

  5. nikunj March 7, 2012 at 3:48 pm

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

     
  6. aunty March 12, 2012 at 12:51 pm

    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.

     
  7. Ankush September 9, 2012 at 11:17 am

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

     
    • James September 9, 2012 at 2:27 pm

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

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

Leave a Reply

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

 

 

 

Theme by HermesThemes

Copyright © 2018 CoderzHeaven. All Rights Reserved

Please wait...

Subscribe to our newsletter

Want to be notified when our article is published? Enter your email address and name below to be the first to know.