Create a basic offline video player

Posted By : Keshav Gupta | 23-Sep-2018

We are going to develop a simple video player application having two screens one is video songs list screen and the other is a player screen. So we will be going step by step. We will be using Content Provider

Step1: We will be fetching all songs file from storage and listing them in a custom listview using ContentProvider media resolver query. There will be a PlayList activity class having ListView in its layout component which will be hosting list of video songs from MediaStore. Code for this will be like:

package com.example.mediakit;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Color;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class Vdeo extends Activity { ListView l1;
Cursor cur;
int count=0;
int videoindex;
String strsong;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.vdeolayout);
l1=(ListView) findViewById(R.id.lv);
fetchsong();
}


@SuppressWarnings("deprecation")
public void fetchsong(){
String[] str={MediaStore.Video.Media._ID,MediaStore.Video.Media.ARTIST,
MediaStore.Video.Media.DISPLAY_NAME,MediaStore.Video.Media.DATA};

cur=managedQuery(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,str,null,null,MediaStore.Video.Media.DISPLAY_NAME + " ASC");
count=cur.getCount();
l1.setAdapter(new A(this));
l1.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
videoindex=cur.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
cur.moveToPosition(arg2);
String strdata=cur.getString(videoindex);
Intent i=new Intent(getApplicationContext(),vpay.class);
i.putExtra("sss", strdata);
startActivity(i);

}
});
}

Step2: We will also create an adapter class whose layout file will contain the view for a title, artist and date and time for video song. The adapter will recreate view on the scroll as ListView works. In this, we are setting data to views from media array list collected in video playlist class from MediaStore.The code will be like.

public class A extends BaseAdapter{

Context con;

public A(Context con)
{
this.con=con;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return count;
}

public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}

@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}

@Override
public View getView(int pos, View view, ViewGroup container) {
// TODO Auto-generated method stub
TextView im=new TextView(con);
videoindex=cur.getColumnIndexOrThrow(MediaStore.Video.Media.DISPLAY_NAME);
cur.moveToPosition(pos);
strsong=cur.getString(videoindex);
im.setText(strsong);
im.setTextColor(Color.BLUE);
return im;
}
}}

Step3: We will now design a Vpay class where the video song will be played on user selection on the list. Basically, the user can now get all media controller features to play, pause and stop feature. There will be an activity named Vpay.class which will be having views like button for stop, start and resume media tracks in its layout.xml file. We will be implementing code for several media controller feature on selection buttons. The code will be like:

package com.example.mediakit;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.MediaController;
import android.widget.VideoView;

public class vpay extends Activity { VideoView vvv;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.videoview);
vvv=(VideoView) findViewById(R.id.vvvv);
Intent i=getIntent();
String ss=i.getStringExtra("sss");
vvv.setVideoPath(ss);
vvv.setMediaController(new MediaController(this));
vvv.start();
}
}

 

Note: This was all about an overall idea that anyone can develop its offline video player. You can add more features.

 

 

 

 

About Author

Author Image
Keshav Gupta

Keshav Gupta is Android Developer in Oodles, he always look forward for new tasks and new things to learn more.

Request for Proposal

Name is required

Comment is required

Sending message..