Android VideoView Introduction:-

 

The VideoView class manages and displays a video file for applications

VideoView can play videos either from resource files, local data or url specified.

Following are the some method used is VideoView:-

  • setVideoURI() – This is used to set the url path. It needs to be parsed as a URI.
  • setVideoPath() – Used for local paths.
  • start() – Used for start Video.
  • stopPlayback()
  • seekTo(int milliSec) – Used for seek Video.
  • pause() – Used for pause Video.
  • resume() – Used for resume Video.
  • isPlaying() – check video is playing or not.
  • canPause()
  • canSeekForward()
  • canSeekBackward()
  • setOnCompletedListener()
  • addSubtitleSource()
  • setMediaController() : Used to add MediaControls on the Video. Pause/Play, Seek
  • getDuration()
  • setOnPreparedListener() : Gets invoked once the video starts.



Permission Required:-

We have to add two permission in manifest.xml

  1. Internet Permission

  2. Read Storage Permission.

now manifest.xml looks likes as below:-

 

Demo Video of Android Videoview:-

manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.androidcreation.videoview">
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

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

</manifest>

 

now add videoview widget in activity_main.xml, put the below code in activity_main.xml


activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>

<VideoView
android:id="@+id/videoview"
android:layout_width="match_parent"
android:layout_marginBottom="50dp"
android:layout_height="wrap_content">
</VideoView>

<LinearLayout
android:layout_width="match_parent"
android:layout_alignParentBottom="true"
android:layout_height="wrap_content">
<Button
android:id="@+id/btnResource"
android:layout_width="0dp"
android:layout_weight=".3"
android:text="Play from Resources"
android:layout_height="wrap_content"></Button>
<Button
android:id="@+id/btnLocal"
android:layout_width="0dp"
android:layout_weight=".3"
android:text="from Local File"
android:layout_height="wrap_content"></Button>
<Button
android:id="@+id/btnUrl"
android:layout_width="0dp"
android:layout_weight=".3"
android:text="Play from URL"
android:layout_height="wrap_content"></Button>
</LinearLayout>
</RelativeLayout>


MediaController Class

The android.widget.MediaController is a view that contains media controls like play/pause, previous, next, fast-forward, rewind etc.

 

finally in the MainActivity.java class add the below code:-

 

package com.androidcreation.videoview;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.MediaController;
import android.widget.VideoView;

import static android.Manifest.permission.ACCESS_FINE_LOCATION;
import static android.Manifest.permission.READ_EXTERNAL_STORAGE;
import static android.Manifest.permission_group.CAMERA;

public class MainActivity extends AppCompatActivity {

Button btnResource,btnLocal,btnUrl;
VideoView videoView;
MediaController mediaController;

private static final int PERMISSION_REQUEST_CODE = 200;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

videoView=findViewById(R.id.videoview);
btnResource=findViewById(R.id.btnResource);
btnLocal=findViewById(R.id.btnLocal);
btnUrl=findViewById(R.id.btnUrl);

//Creating MediaController
mediaController= new MediaController(this);
mediaController.setAnchorView(videoView);
videoView.setMediaController(mediaController);

btnResource.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Uri uri = Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.test);
videoView.setVideoURI(uri);
videoView.start();
}
});
btnLocal.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (checkPermission()) {

playFromStorage();
} else {

requestPermission();
}

}
});
btnUrl.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

videoView.setVideoPath("http://videocdn.bodybuilding.com/video/mp4/62000/62792m.mp4");
videoView.start();
}
});
}
private void playFromStorage()
{
Uri uri= Uri.parse(Environment.getExternalStorageDirectory().getPath()+"/test.mp4");
videoView.setVideoURI(uri);
videoView.requestFocus();
videoView.start();
}
private boolean checkPermission() {
int result = ContextCompat.checkSelfPermission(getApplicationContext(), READ_EXTERNAL_STORAGE);
return result == PackageManager.PERMISSION_GRANTED;
}
private void requestPermission() {

ActivityCompat.requestPermissions(this, new String[]{READ_EXTERNAL_STORAGE}, PERMISSION_REQUEST_CODE);

}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case PERMISSION_REQUEST_CODE:
if (grantResults.length > 0) {
boolean storageAccepted = grantResults[0] == PackageManager.PERMISSION_GRANTED;
if (storageAccepted) {
playFromStorage();
Log.e("MainActivity", "Permission Granted, Now you can access Storage.");
}else {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (shouldShowRequestPermissionRationale(READ_EXTERNAL_STORAGE)) {
showMessageOKCancel("You need to allow access to the permissions",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{ACCESS_FINE_LOCATION, CAMERA},
PERMISSION_REQUEST_CODE);
}
}
});
return;
}
}
}
}
break;
}
}
private void showMessageOKCancel(String message, DialogInterface.OnClickListener okListener) {
new AlertDialog.Builder(MainActivity.this)
.setMessage(message)
.setPositiveButton("OK", okListener)
.setNegativeButton("Cancel", null)
.create()
.show();
}
}
0 CommentsClose Comments

Leave a comment

shares

Newsletter Subscribe

Get the Latest Posts & Articles in Your Email

We Promise Not to Send Spam:)