Android How to play videos from Storage and Filepath

  • by
how to play videos in android 01

Hi,

In this post we will see how we can play video in android from internal memory path or a filepath. The application will be have an android video view control and it will be given video path during runtime. Important, have must grant permission to application access memory path to play video.

Define permission in AndroidManifest.xml:

AndroidManifest.xml:

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

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

<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"
android:screenOrientation="landscape">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

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

</manifest>

Now add android videoview in activity_main.xml layout file.

res>layout>activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000"
android:orientation="horizontal">

<VideoView
android:id="@+id/videoview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" />

</LinearLayout>

android play videos from storage filepath 01
Now edit your MainActivity.java class file as below:

MainActivity.java:

package com.parallel.myvideoplayer;

import android.net.Uri;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.MediaController;
import android.widget.VideoView;

import java.io.File;

public class MainActivity extends AppCompatActivity {
MediaController mediaController;
VideoView videoview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

videoview = findViewById(R.id.videoview);

mediaController= new MediaController(this);
mediaController.setAnchorView(videoview);

String filePath = Environment.getExternalStorageDirectory() + File.separator +
"video" + File.separator + "video1.mp4";

videoview.setMediaController(mediaController);
videoview.setVideoURI(Uri.parse(filePath));
videoview.start();

}
}

DOWNLOAD SOURCE CODE.

Also see:

How to play videos using Exoplayer in Android?


  1. Base64Encode.io - Realtime Encode your string to Base64 format.
  2. Base64Decode.io - Realtime Decode Base64 data to plain text.
  3. Md5Hash.io - Realtime Encoding in Md5Hash.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.