Play Video from SDCard

Friends today we are going to learn how to play video from sd card with step by step.before starting it we learn about Video View and Medea Controller.


VideoView: This class is basically used to for the display of Video File. VideoView can also be used to load images for the various sources, having information about their measurements computation from the video so that images can be used in any kind of layout manager. Here are below important methods of this class:

void start(): This method is used to start the playback of video file.
void pause(): This method is used to pause the current playback.
boolean canPause(): This method will tell whether video view is able to pause.
boolean canSeekForward(): This method will return true value if video view is able to seek forward.
int getDuration(): This method is used to get the total duration of video view.
boolean isPlaying(): This method will return true value if the current video view is in play state.
void resume(): This method is used to play the resumed file.


MediaController: MediaController class is used to provide the controls for the video playback. It is used with the VideoView most of. MediaController contains the PAUSE, PLAY, FORWARD, REWIND, SEEKBAR which will sync with state of MediaPlayer.

Here are the below methods of this class which are mostly used:

void hide(): This method is use to hide the controls from the screen.
Boolean onTouchEvent(MotionEvent evnt): This method is used to handle the touch screen events.
void setEnabled(boolean value): This method is used to set the Enable state of this view.
void show(int timeOut): This method is used to set the time to show the controller on the screen of current activity.
void show(): This method is used to show the controller on the screeen of current activity.

Full Source code Example

public class video extends Activity{
 
    VideoView video_view;
    String ex_name;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.eccryption);
        video_view = (VideoView) findViewById(R.id.videoView1);
 
        ex_name = getIntent().getExtras().getString("video_name");
 
        MediaController mediaController = new MediaController(this);
        mediaController.setAnchorView(video_view);
        video_view.setMediaController(new MediaController(this));
        handler.sendEmptyMessage(1);
 
    }
 
    Handler handler = new Handler(){
 
        public void handleMessage(Message msg){
 
            int pos=msg.what;
            if (pos == 1){
 
                video_view.setVideoPath(Environment.getExternalStorageDirectory()+"/"+ex_name+".mp4");
                video_view.requestFocus();
                video_view.start();
 
                Log.d("Before Video Finish", "i m in before video finish");
                video_view.setOnCompletionListener(new OnCompletionListener() {
                    @Override
                    public void onCompletion(MediaPlayer mp) {
                        finish();
                    }
                });
            }
        }
    };