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. <Button
  7. android:id="@+id/playvideoplayer"
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:text=" PLAY "/>
  11. <Button
  12. android:id="@+id/stopvideoplayer"
  13. android:layout_width="fill_parent"
  14. android:layout_height="wrap_content"
  15. android:text=" STOP "/>
  16. <VideoView
  17. android:id="@+id/viewvideo"
  18. android:layout_width="fill_parent"
  19. android:layout_height="wrap_content"/>
  20. </LinearLayout>
Step 2
Create a Java class file with the following:
  1. package com.example.thread;
  2. import android.app.Activity;
  3. import android.graphics.PixelFormat;
  4. import android.media.MediaPlayer;
  5. import android.net.Uri;
  6. import android.os.Bundle;
  7. import android.view.SurfaceHolder;
  8. import android.view.SurfaceView;
  9. import android.view.View;
  10. import android.view.View.OnClickListener;
  11. import android.widget.Button;
  12. import android.widget.VideoView;
  13. //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.
  14. public class MainActivity extends Activity implements SurfaceHolder.Callback{
  15. MediaPlayer mediaPlayer;
  16. SurfaceView surfaceView;
  17. SurfaceHolder surfaceHolder;
  18. boolean pausing = false;
  19. Button stopVideo;
  20. // 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/
  21. @Override
  22. public void onCreate(Bundle savedInstanceState) {
  23. super.onCreate(savedInstanceState);
  24. setContentView(R.layout.activity_main);
  25. Button PlayVideo = (Button)findViewById(R.id.playvideoplayer);
  26. stopVideo = (Button)findViewById(R.id.stopvideoplayer);
  27. getWindow().setFormat(PixelFormat.UNKNOWN);
  28. //Displays a video file.
  29. final VideoView mVideo = (VideoView)findViewById(R.id.viewvideo);
  30. //set the playvideo button on its clickListener. So when you click on the button videoview will display a video
  31. PlayVideo.setOnClickListener(new Button.OnClickListener(){
  32. @Override
  33. public void onClick(View v) {
  34. VideoView mVideo = (VideoView)findViewById(R.id.viewvideo);
  35. //path of your video file.
  36. String Path = "android.resource://com.example.thread/"+R.raw.k;
  37. Uri uri = Uri.parse(Path);
  38. mVideo.setVideoURI(uri);
  39. mVideo.requestFocus();
  40. mVideo.setVisibility(View.VISIBLE);
  41. mVideo.start();
  42. }});
  43. stopVideo.setOnClickListener(new OnClickListener() {
  44. @Override
  45. public void onClick(View v) {
  46. // TODO Auto-generated method stub
  47. // on button click the video will be stop
  48. mVideo.stopPlayback();
  49. mVideo.setVisibility(View.GONE);
  50. }
  51. });
  52. }
  53. @Override
  54. public void surfaceChanged(SurfaceHolder holder, int format, int width,
  55. int height) {
  56. // TODO Auto-generated method stub
  57. }
  58. @Override
  59. public void surfaceCreated(SurfaceHolder holder) {
  60. // TODO Auto-generated method stub
  61. }
  62. @Override
  63. public void surfaceDestroyed(SurfaceHolder holder) {
  64. // TODO Auto-generated method stub
  65. }
  66. }
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. <uses-sdk
  7. android:minSdkVersion="8"
  8. android:targetSdkVersion="18" />
  9. <application
  10. android:allowBackup="true"
  11. android:icon="@drawable/ic_launcher"
  12. android:label="@string/app_name"
  13. android:theme="@style/AppTheme" >
  14. <activity
  15. android:name="com.example.thread.MainActivity"
  16. android:label="@string/app_name" >
  17. <intent-filter>
  18. <action android:name="android.intent.action.MAIN" />
  19. <category android:name="android.intent.category.LAUNCHER" />
  20. </intent-filter>
  21. </activity>
  22. </application>
  23. </manifest>
Step 4
When you run the application:
videoview
Click on the play button.
StopVideo
Click on "Stop"; your video will be hidden.