Video Player Android

The video files can be played in android by using the MediaController and VideoView classes.

MediaController class:

The media controls like play/pause, previous, next, fast-forward, rewind, etc., are contained in the android.widget.MediaController. It is a view.

VideoView class:

To play and control the video player, the methods of the android.widget.VideoView class are used. Some of the most common methods of the VideoView class are:

Method Uses
public void setMediaController(MediaController controller) Used to set the media controller to the video view.
public void setVideoURI (Uri uri) Used to set the URI of the video file.
public void start() Used to start the video view.
public void stopPlayback() Used to stop the playback.
public void pause() Used to pause the playback.
public void suspend() Used to suspend the playback.
public void resume() Used to resume the playback.
public void seekTo(int millis) Used to seek a specific time in milliseconds.

activity_main.xml:

In the activity_main.xml file, we will drag the VideoView from the palette.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >
 
    <VideoView
        android:id="@+id/videoView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true" />
 
</RelativeLayout>

Activity class:(File: MainActivity.java)

In the MainActivity.java file, we will write the code to play a .mp4 file. The file is either located inside the SDCard or the media directory.

package com.example.radioapp;
 
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.os.Environment;
import android.view.Menu;
import android.widget.MediaController;
import android.widget.VideoView;
 
public class MainActivity extends Activity {
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        VideoView videoView =(VideoView)findViewById(R.id.videoView1);
 
        //Creating MediaController
        MediaController mediaController= new MediaController(this);
        mediaController.setAnchorView(videoView);
 
        //specify the location of media file
        Uri uri=Uri.parse(Environment.getExternalStorageDirectory().getPath()+"/media/musicplay.mp4");
 
        //Setting MediaController and URI, then starting the videoView
        videoView.setMediaController(mediaController);
        videoView.setVideoURI(uri);
        videoView.requestFocus();
        videoView.start();
 
    }
 
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu, menu);
        return true;
    }
 
}