PlayVideo File Using VideoPlayer in Android

Introduction

 
This article explains how to play a video using the Video Player in Android.
 
This application will show you a video when you click on the "Start video" button and hide it when you click on the Stop button. For this, you will store a 3gp video file inside the raw folder that you will call on a button click.
 
Step 1
 
Create an XML file with the following. In this file you will use two buttons to start the video and to stop the video. Use a VideoView that will show the video on button click at runtime.
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent">  
  6.    
  7.   <Button  
  8.     android:id="@+id/playvideoplayer"  
  9.     android:layout_width="fill_parent"  
  10.     android:layout_height="wrap_content"  
  11.     android:text=" PLAY  "/>  
  12.      
  13.    <Button  
  14.     android:id="@+id/stopvideoplayer"  
  15.     android:layout_width="fill_parent"  
  16.     android:layout_height="wrap_content"  
  17.     android:text=" STOP "/>  
  18.      
  19.    <VideoView  
  20.     android:id="@+id/viewvideo"  
  21.     android:layout_width="fill_parent"  
  22.     android:layout_height="wrap_content"/>  
  23.    
  24. </LinearLayout> 
Step 2
 
Create a Java class file with the following:
  1. package com.example.thread;  
  2.    
  3. import android.app.Activity;  
  4. import android.graphics.PixelFormat;  
  5. import android.media.MediaPlayer;  
  6. import android.net.Uri;  
  7. import android.os.Bundle;  
  8. import android.view.SurfaceHolder;  
  9. import android.view.SurfaceView;  
  10. import android.view.View;  
  11. import android.view.View.OnClickListener;  
  12. import android.widget.Button;  
  13. import android.widget.VideoView;  
  14.    
  15. //In your class file, you will extend the Activity class that provides you the onCreate() on which the compiler comes first. Now implements the SurfaceHolder interface to play a video.  
  16.   
  17. public class MainActivity extends Activity implements SurfaceHolder.Callback{  
  18.    
  19.     MediaPlayer mediaPlayer;  
  20.     SurfaceView surfaceView;  
  21.     SurfaceHolder surfaceHolder;  
  22.     boolean pausing = false;  
  23.     Button stopVideo;  
  24.       
  25.     // this method is provided by the Activity class on which the compiler comes first when you run the application.Called when the activity is first created/  
  26.     @Override  
  27.     public void onCreate(Bundle savedInstanceState) {  
  28.         super.onCreate(savedInstanceState);  
  29.         setContentView(R.layout.activity_main);  
  30.           
  31.         Button PlayVideo = (Button)findViewById(R.id.playvideoplayer);  
  32.         stopVideo = (Button)findViewById(R.id.stopvideoplayer);   
  33.         getWindow().setFormat(PixelFormat.UNKNOWN);  
  34.          
  35.         //Displays a video file.    
  36.         final VideoView mVideo = (VideoView)findViewById(R.id.viewvideo);  
  37.           
  38.      //set the playvideo button on its clickListener. So when you click on the button videoview will display a video  
  39.    
  40.          PlayVideo.setOnClickListener(new Button.OnClickListener(){  
  41.    
  42.             @Override  
  43.             public void onClick(View v) {  
  44.               
  45.                 VideoView mVideo = (VideoView)findViewById(R.id.viewvideo);                     
  46.            
  47.                 //path of your video file.  
  48.                  String Path = "android.resource://com.example.thread/"+R.raw.k;                    
  49.                     Uri uri = Uri.parse(Path);  
  50.                     mVideo.setVideoURI(uri);  
  51.                     mVideo.requestFocus();  
  52.                     mVideo.setVisibility(View.VISIBLE);  
  53.                     mVideo.start();  
  54.             }});  
  55.       
  56.     stopVideo.setOnClickListener(new OnClickListener() {  
  57.                
  58.               @Override  
  59.               public void onClick(View v) {  
  60.                      // TODO Auto-generated method stub             
  61.                // on button click the video will be stop       
  62.                mVideo.stopPlayback();  
  63.                mVideo.setVisibility(View.GONE);  
  64.               }  
  65.        });  
  66.     }  
  67.      
  68.     @Override  
  69.     public void surfaceChanged(SurfaceHolder holder, int format, int width,  
  70.             int height) {  
  71.         // TODO Auto-generated method stub  
  72.     }  
  73.    
  74.     @Override  
  75.     public void surfaceCreated(SurfaceHolder holder) {  
  76.         // TODO Auto-generated method stub  
  77.     }  
  78.    
  79.     @Override  
  80.     public void surfaceDestroyed(SurfaceHolder holder) {  
  81.         // TODO Auto-generated method stub  
  82.           
  83.     }  
Step 3
 
Android Manifest.xml file with the following:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.example.thread"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.    
  7.     <uses-sdk  
  8.         android:minSdkVersion="8"  
  9.         android:targetSdkVersion="18" />  
  10.    
  11.     <application  
  12.         android:allowBackup="true"  
  13.         android:icon="@drawable/ic_launcher"  
  14.         android:label="@string/app_name"  
  15.         android:theme="@style/AppTheme" >  
  16.         <activity  
  17.             android:name="com.example.thread.MainActivity"  
  18.             android:label="@string/app_name" >  
  19.             <intent-filter>  
  20.                 <action android:name="android.intent.action.MAIN" />  
  21.    
  22.                 <category android:name="android.intent.category.LAUNCHER" />  
  23.             </intent-filter>  
  24.         </activity>  
  25.     </application>  
  26. </manifest> 
Step 4
 
When you run the application:
 
videoview
 
Click on the play button.
 
StopVideo
 
Click on "Stop"; your video will be hidden.