Android MediaController example tutorial

Play online videos with custom http url and display play,pause,next, previous buttons with video play time with horizontal progress bar.

MediaController class is very important class for android developer who wish to create online media player android application because this class allows us to gain more control on VideoView widget and the combination of VideoView and MediaController, an  app developer can easily build up online media player application with all the play, pause, next, previous buttons with displaying video current state play time including video total time and also gives us horizontal progress bar to control video playing like real media players. So here is the complete step by step tutorial for Android MediaController example tutorial.

android-project-download-code-button

Android MediaController example tutorial.

Please add internet permission inside AndroidManifest.xml file.

 <uses-permission android:name="android.permission.INTERNET" />

Code for MainActivity.java file.

 package com.mediacontroller_android_examples.com;
import android.app.Activity;
import android.os.Bundle;
import android.widget.MediaController;
import android.widget.VideoView;

public class MainActivity extends Activity {

 VideoView videoview ;
 MediaController mediacontroller;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 videoview = (VideoView)findViewById(R.id.videoView1);
 mediacontroller = new MediaController(MainActivity.this);
 
 videoview.setVideoPath(
 "https://www.android-examples.com/wp-content/uploads/2016/01/sample_video.3gp");
 
 mediacontroller.setAnchorView(videoview);
 
 videoview.setMediaController(mediacontroller);
 
 videoview.start();

 }
}

 

Code for activity_main.xml layout file.

<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"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.mediacontroller_android_examples.com.MainActivity"
 android:background="#02bf95" >

 <VideoView
 android:id="@+id/videoView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_centerVertical="true" />

</RelativeLayout>

Code for AndroidManifest.xml file.

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

 <uses-sdk
 android:minSdkVersion="8"
 android:targetSdkVersion="21" />

 <uses-permission android:name="android.permission.INTERNET" />
 <application
 android:allowBackup="true"
 android:icon="@drawable/ic_launcher"
 android:label="@string/app_name"
 android:theme="@style/AppTheme" >
 <activity
 android:name=".MainActivity"
 android:label="@string/app_name" >
 <intent-filter>
 <action android:name="android.intent.action.MAIN" />

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

</manifest>

Screenshot:

Android MediaController example tutorial

Click here to download Android MediaController example tutorial project with source code.

Leave a Reply

Your email address will not be published. Required fields are marked *