31 March 2015

Code for MediaController with VideoView in Android

What we will make ?
We will play a video with MediaController .

What we will need ?
  • VideoView in activity_main.xml
  • Adding Code with MediaController in MainActivity.java
Step 1 : Create a New Android Application . Go to File->New->Android Application Project.

Step 2 : Give a name to your App (MediaControllerDemo) and a package name (mia) as shown below. Click Next continuously then click Finish.

Step 3 : Make a new folder named raw in res. Right-click on res->New->Folder. Name it raw. Now copy the video file (video.mp4) you want to use for VideoView in raw folder.

Step 4 : Add VideoView in a LinearLayout in activty_main.xml.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout01"
android:layout_height="fill_parent"
android:paddingLeft="2dp"
android:paddingRight="2dp"
android:paddingTop="2dp"
android:paddingBottom="2dp"
android:layout_width="fill_parent"
android:orientation="vertical">
<VideoView
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:id="@+id/VideoView">
</VideoView>
</LinearLayout>

Step 5 : Open MainActivity.java from src->com.mia.mediacontrollerdemo in Project Explorer. Copy the code below in it.

MainActivity.java
package com.mia.mediacontrollerdemo;

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.widget.MediaController;
import android.widget.VideoView;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        VideoView videoView = (VideoView) findViewById(R.id.VideoView);
        MediaController mediaController = new MediaController(this);
        mediaController.setAnchorView(videoView);
        String uriPath = "android.resource://com.mia.mediacontrollerdemo/"+R.raw.video;
        Uri video = Uri.parse(uriPath);
        videoView.setMediaController(mediaController);
        videoView.setVideoURI(video);
        videoView.start();
    }
}

Step 6 : Run the Project.

Explanation Of the Code :

Explanation from activity_main.xml :
  • We add a VideoView with id VideoView in a LinearLayout.
<VideoView
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:id="@+id/VideoView">
</VideoView>

Explanation from MainActivity.java :
  • We make a variable videoView of type VideoView and connect it to VideoView element from layout activity_main.xml.
VideoView videoView = (VideoView) findViewById(R.id.VideoView);
  • We make a MediaController and connect it to our VideoView.
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(videoView);
  • Store path of video in String variable uriPath and convert it into Uri type using parse().
String uriPath = "android.resource://com.mia.mediacontrollerdemo/"+R.raw.video;
Uri video = Uri.parse(uriPath);
  • Set the MediaController for the video using setMediaController() and set the video path and then start it using start().
videoView.setMediaController(mediaController);
videoView.setVideoURI(video);
videoView.start();


ALSO

Stay Tuned with Made In Android

Previous Page Next Page Home
Top