Hi,
In this post we will see how we can play videos in Android using Android VideoView control. The application will play a video from file path (Android’s Internal memory or External memory i.e. SDCard). We will have to manually grant Android permissions to grant access to read phone’s storage. So let’s begin.
Android Viewview is by default available in Android framework. We can use this to fetch video and play it on Android without any additional libraries in our application. Additionally we can also stream video from youtube or using video url on video player to it on Android. In this tutorial we will see how we fetch video from internal storage and play it on our video view application.
Note: Please add a video with name video1.mp4 in your video folder of your Android phone.
First thing first, add permissions in your AndroidManifest file
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>
Open your activity_main.xml file and add Android Videoview control to it. On this we will play our video.
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>
Now, open your MainActivity.java file and edit it as below to successfully play videos on our Android app.
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(); } }
Now run the application. You must have a video in your phone’s internal memory in video folder with name video1.mp4.
Also see:
How to play video in Android using Android Exoplayer?
Download source code for Android Exoplayer.