Video Player in Android.


Help of MediaController and VideoView classes, we can play the video files in android phone.
MediaController is a view that contains media controls like play/pause, previous, next, fast-forward, rewind etc. VideoView class provides methods to play and control the video player.

Create a new Andorid Project

Open AndroidManifest.xml file and add following permission.

AndroidManifest.xml


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.nk.permissionrequire">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:screenOrientation="portrait">

        </activity>
        <activity
            android:name=".PermissionActivity"
            android:theme="@style/Theme.AppCompat.NoActionBar"
            android:screenOrientation="portrait">

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

        </activity>
    </application>

</manifest>
	
	

Create a new Activity that take a permission. Permission ask before a main class called.

PermissionActivity.java

									   
package com.nk.permissionrequire;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import java.util.ArrayList;
import java.util.List;

public class PermissionActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_permission);
        context = this;
        checkPermissions();
    }

    Context context;
    Boolean per = false;
    String[] permissions = new String[]{
            Manifest.permission.INTERNET,
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
    };

    private boolean checkPermissions() {
        int result;
        List listPermissionsNeeded = new ArrayList<>();
        for (String p : permissions) {
            result = ContextCompat.checkSelfPermission(this, p);
            if (result != PackageManager.PERMISSION_GRANTED) {
                listPermissionsNeeded.add(p);
               Log.w("-----------", "granteddddd ddddddddddd ");
            } else {
                Log.w("-----------", "granteddddd elseeeeeeeeeeeeeeee");

            }
        }
   if (!listPermissionsNeeded.isEmpty()) {
      ActivityCompat.requestPermissions(this, listPermissionsNeeded.toArray
	  (new String[listPermissionsNeeded.size()]), 100);
            Log.w("-----------", "granteddddd  iffff");
            return false;
        } else {
            Log.w("-----------", "granteddddd 666666666(");
            startActivity(new Intent(context, MainActivity.class));
            finish();
        }
        return true;
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, 
	@NonNull String[] permissions, @NonNull int[] grantResults) {
	
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 100) {
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // do something
                Log.w("-----------", "granteddddd ");
                startActivity(new Intent(context, MainActivity.class));
                finish();
            }
            return;
        }
    }
}

No need to anything implement in this xml file.

activity_permission.xml

									   						   
   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

</LinearLayout>

It is main xml file you can perform any layout work here.

activity_main.xml

						   
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_all_video_show"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="5dp">

        <TextView
            android:id="@+id/no_video_txt"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:padding="20dp"
            android:text="No Video Found"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:visibility="gone" />

        <ListView
            android:id="@+id/PhoneVideoList"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:drawSelectorOnTop="true"
            android:scrollingCache="true"
            android:animationCache="true"
            android:scrollbars="none" />

    </RelativeLayout>

</LinearLayout>
   

It is main JAVA class file you can perform any task here.

MainActivity.java

									   						   
package com.nk.permissionrequire;

import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.io.File;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    ListView videolist;
    int count;
    String thumbPath;
    Context context;
    String[] thumbColumns = {MediaStore.Video.Thumbnails.DATA,
      	MediaStore.Video.Thumbnails.VIDEO_ID};
		
    DisplayMetrics metrics;
    MainActivity.VideoListAdapter videoListAdapter;
    private Cursor videoCursor = null;
    private int deviceHeight;
    private int deviceWidth;
    TextView no_video_txt;

    public static String convertBytes(long size) {
        if (size <= 0) return "0 B";
        final String[] units = new String[]{"B", "KB", "MB", "GB", "TB"};
        int digitGroups = (int) (Math.log10(size) / Math.log10(1024));
        return new DecimalFormat("#,##0.##").format
		 (size / Math.pow(1024, digitGroups)) + " " + units[digitGroups];
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context = this;
        metrics = new DisplayMetrics();
        getScreenSize();
        videolist = (ListView) findViewById(R.id.PhoneVideoList);
        no_video_txt = (TextView) findViewById(R.id.no_video_txt);
    }

    private void initialization() {
        String[] videoProjection = {MediaStore.Video.Media._ID, MediaStore.Video.Media.DATA,
        MediaStore.Video.Media.DISPLAY_NAME, MediaStore.Video.Media.SIZE, 
	MediaStore.Video.Media.DATE_TAKEN, MediaStore.Video.VideoColumns.DURATION};
				
        videoCursor = managedQuery(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, 
		            videoProjection, null, null, null);

        if (videoCursor != null && videoCursor.getCount() > 0) {
            count = videoCursor.getCount();
            videoCursor.moveToFirst();
            no_video_txt.setVisibility(View.GONE);
        } else {
            count = 0;
        }
        if (count == 0) {
            no_video_txt.setVisibility(View.VISIBLE);
            videolist.setVisibility(View.GONE);
        }
        videoListAdapter = new MainActivity.VideoListAdapter(context);
        videolist.setAdapter(videoListAdapter);
        videoListAdapter.notifyDataSetChanged();
    }

    @Override
    protected void onResume() {
        super.onResume();
        initialization();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
    }

    public class VideoListAdapter extends BaseAdapter {
        ContentResolver contentResolver;
        private Context vContext;
        private int videoColumnIndex;
        ArrayList arrayList;

        public VideoListAdapter(Context vContext) {
            this.vContext = vContext;
            this.arrayList = new ArrayList<>();
        }

        public int getCount() {
            Log.w("-------------", "All video videoCursor.getCount() : " + count);
            return count;
        }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(final int position, View convertView, ViewGroup parent) {
            metrics = new DisplayMetrics();

    convertView = LayoutInflater.from(vContext).inflate(R.layout.videolayout, parent, false);
    final LinearLayout video_ll = (LinearLayout) convertView.findViewById(R.id.video_ll);
    LinearLayout main_name_ll = (LinearLayout) convertView.findViewById(R.id.main_name_ll);
    TextView video_name = (TextView) convertView.findViewById(R.id.video_name);
    TextView video_path = (TextView) convertView.findViewById(R.id.video_path);
    TextView video_size = (TextView) convertView.findViewById(R.id.video_size);
    TextView video_date = (TextView) convertView.findViewById(R.id.video_date);
    TextView video_duration = (TextView) convertView.findViewById(R.id.video_duration);
    ImageView thumbImage = (ImageView) convertView.findViewById(R.id.img_video);
    final String str_video_name, str_video_path;
        videoColumnIndex = videoCursor.getColumnIndexOrThrow
	       (MediaStore.Video.Media.DISPLAY_NAME);
				   
            videoCursor.moveToPosition(position);
            final String str_name = videoCursor.getString(videoColumnIndex);
            video_name.setText(videoCursor.getString(videoColumnIndex));
            str_video_name = videoCursor.getString(videoColumnIndex);
            videoColumnIndex = videoCursor.getColumnIndexOrThrow
			      (MediaStore.Video.Media.DATA);
				  
            videoCursor.moveToPosition(position);
            final String str_Data = videoCursor.getString(videoColumnIndex);
            video_path.setText(videoCursor.getString(videoColumnIndex));
            str_video_path = videoCursor.getString(videoColumnIndex);

            videoColumnIndex = videoCursor.getColumnIndexOrThrow
			      (MediaStore.Video.Media.SIZE);
				  
            videoCursor.moveToPosition(position);
            final long videoz_size = Long.parseLong
			     (videoCursor.getString(videoColumnIndex));
				 
            final String str_size = convertBytes(Long.parseLong
			     (videoCursor.getString(videoColumnIndex)));
				 
            video_size.setText("" + convertBytes(Long.parseLong
			      (videoCursor.getString(videoColumnIndex))) + "");

            videoColumnIndex = videoCursor.getColumnIndexOrThrow
			        (MediaStore.Video.Media.DATE_TAKEN);
            videoCursor.moveToPosition(position);

            SimpleDateFormat df = new SimpleDateFormat("dd/MMM/yyyy");
            long ddd = Long.parseLong(videoCursor.getString(videoColumnIndex));
            String result = df.format(ddd);
            final String str_date = result;
            video_date.setText("" + result);

            videoColumnIndex = videoCursor.getColumnIndexOrThrow 
			      (MediaStore.Video.VideoColumns.DURATION);
				  
            videoCursor.moveToPosition(position);
            int duration = Integer.parseInt(videoCursor.getString(videoColumnIndex));
            int timeInmillisec = (duration);
            long duration1 = timeInmillisec / 1000;
            long hours = duration1 / 3600;
            long minutes = (duration1 - hours * 3600) / 60;
            long seconds = duration1 - (hours * 3600 + minutes * 60);
            final String str_duration;
            if (hours == 00) {
                str_duration = "" + twoDigitString((int) minutes) + " : "
          				+ twoDigitString((int) seconds);
            } else {
          str_duration = "" + twoDigitString((int) hours) + " : " +
    		twoDigitString((int) minutes) + " : " + twoDigitString((int) seconds);
            }

            video_duration.setText(str_duration);

            videoColumnIndex = videoCursor.getColumnIndexOrThrow 
			          (MediaStore.Video.Thumbnails.DATA);
            videoCursor.moveToPosition(position);

            int videoId = videoCursor.getInt(videoCursor.getColumnIndexOrThrow
			        (MediaStore.Video.Media._ID));
					
            Cursor videoThumbnailCursor = managedQuery
			        (MediaStore.Video.Thumbnails.EXTERNAL_CONTENT_URI,
					
            thumbColumns, MediaStore.Video.Thumbnails.VIDEO_ID + "=" + videoId, null, null);

            if (videoThumbnailCursor.moveToFirst()) {
                thumbPath = videoThumbnailCursor.getString
	    (videoThumbnailCursor.getColumnIndex(MediaStore.Video.Thumbnails.DATA));
            }
  video_path.setTextSize(TypedValue.COMPLEX_UNIT_PX, DetermineTextSize.determineTextSize
  (video_path.getTypeface(), (float) (deviceHeight * 2.2 / 100)));
  
  video_duration.setTextSize(TypedValue.COMPLEX_UNIT_PX, DetermineTextSize.determineTextSize
			(video_duration.getTypeface(), deviceHeight * 3 / 100));
  
  video_size.setTextSize(TypedValue.COMPLEX_UNIT_PX, DetermineTextSize.determineTextSize
          (video_size.getTypeface(), (float) (deviceHeight * 2.5 / 100)));
  
  video_date.setTextSize(TypedValue.COMPLEX_UNIT_PX, DetermineTextSize.determineTextSize
          (video_date.getTypeface(), (float) (deviceHeight * 2.2 / 100)));

      RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams((int) 
			            (deviceWidth / 5), deviceHeight / 11);
            thumbImage.setLayoutParams(params);
      RelativeLayout.LayoutParams params_duration = new RelativeLayout.LayoutParams((int) 
			            (deviceWidth / 8), deviceHeight / 25);

            // video_duration.setLayoutParams(params_duration);

      LinearLayout.LayoutParams params_main_ll = new LinearLayout.LayoutParams((int)
               			(deviceWidth / 1.3), deviceHeight / 11);
            main_name_ll.setLayoutParams(params_main_ll);

      LinearLayout.LayoutParams params_ll = new LinearLayout.LayoutParams
			             (deviceWidth / 1, deviceHeight / 11);
            video_ll.setLayoutParams(params_ll);
            contentResolver = getContentResolver();

            try {
                Glide.with(vContext)
                        .load(new File(videoCursor.getString(videoColumnIndex)))
                        .placeholder(R.drawable.no_thumbnail)
                        .error(R.drawable.no_thumbnail)
                        .into(thumbImage);
            } catch (Exception e) {
                e.printStackTrace();
            }

            final String get_video_data = str_video_name + ",>" + str_video_path;

            video_ll.setTag(get_video_data);

            video_ll.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String itemData = (String) v.getTag();
                    Log.w("-------------", "Video Data on CLick to play : " + itemData);
    startActivity(new Intent(vContext, PlayActivity.class).putExtra("video", itemData));
                }
            });
            return convertView;
        }

        private String twoDigitString(int number) {
            if (number == 0) {
                return "00";
            }
            if (number / 10 == 0) {
                return "0" + number;
            }
            return String.valueOf(number);
        }
    }
    public void getScreenSize() {
        WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
        wm.getDefaultDisplay().getMetrics(metrics);
        deviceHeight = metrics.heightPixels;
        deviceWidth = metrics.widthPixels;
    }
}

It is main xml file you can perform any layout work here.

activity_play.xml

						   
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <VideoView
        android:id="@+id/play_video"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_centerInParent="true"
        android:focusable="false"
        android:focusableInTouchMode="false" />

</RelativeLayout>
   

Video Player Java Class

PlayActivity.java

						   
package com.nk.permissionrequire;

import android.content.Context;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.media.AudioManager;
import android.media.MediaMetadataRetriever;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.Window;
import android.widget.MediaController;
import android.widget.VideoView;

public class PlayActivity extends AppCompatActivity implements 
               GestureDetection.SimpleGestureListener {

    Context context;
    VideoView play_video;
    AudioManager audioManager;
    GestureDetection detector;
    int currentPosition;
    int currentVolume;
    MediaController mediaController;
    MediaMetadataRetriever retriever;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        supportRequestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_play);
        context = this;
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setBackgroundDrawable 
		      (new ColorDrawable(Color.parseColor("#00212121")));
			  
        getSupportActionBar().setElevation(0);
        play_video = (VideoView) findViewById(R.id.play_video);
        audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
        detector = new GestureDetection(this, this);

        String video_data = getIntent().getStringExtra("video");
        getSupportActionBar().setTitle(video_data.toString().split(",>")[0]);

        Uri video = Uri.parse(video_data.toString().split(",>")[1]);
        play_video.setVideoURI(video);
        mediaController = new MediaController(context);

        retriever = new MediaMetadataRetriever();
        retriever.setDataSource("" + video_data.toString().split(",>")[1]);
        int ori = Integer.valueOf(retriever.extractMetadata 
		               (MediaMetadataRetriever.METADATA_KEY_VIDEO_ROTATION));
        retriever.release();

        if (ori == 90) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        } else {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
        }

        mediaController.setAnchorView(play_video);
        mediaController.setMediaPlayer(play_video);
        play_video.setMediaController(mediaController);

        play_video.requestFocus();
        play_video.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                play_video.start();

                mp.setOnVideoSizeChangedListener(new MediaPlayer.OnVideoSizeChangedListener() {
                    @Override
                    public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {

                    }
                });
            }
        });
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent me) {
        // Call onTouchEvent of SimpleGestureFilter class
        this.detector.onTouchEvent(me);
        return super.dispatchTouchEvent(me);
    }

    @Override
    public void onSwipe(int direction) {
        // TODO Auto-generated method stub

        switch (direction) {

            case GestureDetection.SWIPE_LEFT:

                currentPosition = play_video.getCurrentPosition();
                currentPosition = play_video.getCurrentPosition() + 5000;
                play_video.seekTo(currentPosition);
                break;

            case GestureDetection.SWIPE_RIGHT:

                currentPosition = play_video.getCurrentPosition();
                currentPosition = play_video.getCurrentPosition() - 5000;
                play_video.seekTo(currentPosition);
                break;

            case GestureDetection.SWIPE_DOWN:

                currentVolume = audioManager
                        .getStreamVolume(AudioManager.STREAM_MUSIC);
                audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
                        currentVolume - 1, 0);
                break;
            case GestureDetection.SWIPE_UP:

                currentVolume = audioManager
                        .getStreamVolume(AudioManager.STREAM_MUSIC);
                audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
                        currentVolume + 1, 0);
                break;
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
   

Download Code

Android Develepment

  • Android

  • Android Studio

  • Android development