Android Video Player Example

  • 0

Android Video Player Example

Category : Uncategorized

By the help of MediaController and VideoView classes, we can play the video files in android.

MediaController class

The android.widget.MediaController is a view that contains media controls like play/pause, previous, next, fast-forward, rewind etc.

VideoView class

The android.widget.VideoView class provides methods to play and control the video player. The commonly used methods of VideoView class are as follows:

Method Description
public void setMediaController(MediaController controller) sets the media controller to the video view.
public void setVideoURI (Uri uri) sets the URI of the video file.
public void start() starts the video view.
public void stopPlayback() stops the playback.
public void pause() pauses the playback.
public void suspend() suspends the playback.
public void resume() resumes the playback.
public void seekTo(int millis) seeks to specified time in miliseconds.

activity_main.xml

Drag the VideoView from the pallete, now the activity_main.xml file will like this:

File: activity_main.xml
  1. <RelativeLayout xmlns:androclass=“http://schemas.android.com/apk/res/android”
  2.     xmlns:tools=“http://schemas.android.com/tools”
  3.     android:layout_width=“match_parent”
  4.     android:layout_height=“match_parent”
  5.     tools:context=“.MainActivity” >
  6.     <VideoView
  7.         android:id=“@+id/videoView1”
  8.         android:layout_width=“wrap_content”
  9.         android:layout_height=“wrap_content”
  10.         android:layout_alignParentLeft=“true”
  11.         android:layout_centerVertical=“true” />
  12. </RelativeLayout>

Activity class

Let’s write the code of to play the video file. Here, we are going to play 1.mp4 file located inside the sdcard/media directory.

File: MainActivity.java
  1. package com.example.video1;
  2. import android.net.Uri;
  3. import android.os.Bundle;
  4. import android.app.Activity;
  5. import android.view.Menu;
  6. import android.widget.MediaController;
  7. import android.widget.VideoView;
  8. public class MainActivity extends Activity {
  9.     @Override
  10.     protected void onCreate(Bundle savedInstanceState) {
  11.         super.onCreate(savedInstanceState);
  12.         setContentView(R.layout.activity_main);
  13.         VideoView videoView =(VideoView)findViewById(R.id.videoView1);
  14.                 //Creating MediaController
  15.         MediaController mediaController= new MediaController(this);
  16.             mediaController.setAnchorView(videoView);
  17.               //specify the location of media file
  18.            Uri uri=Uri.parse(Environment.getExternalStorageDirectory().getPath()+“/media/1.mp4”);
  19.               //Setting MediaController and URI, then starting the videoView
  20.            videoView.setMediaController(mediaController);
  21.            videoView.setVideoURI(uri);
  22.            videoView.requestFocus();
  23.            videoView.start();
  24.     }
  25.     @Override
  26.     public boolean onCreateOptionsMenu(Menu menu) {
  27.         // Inflate the menu; this adds items to the action bar if it is present.
  28.         getMenuInflater().inflate(R.menu.activity_main, menu);
  29.         return true;
  30.     }
  31. }

Leave a Reply