Introduction
This article shows how to play videos from URLs directly in our Android Video Player application. Here, we are taking the VideoView control to play the videos. This player includes a media controller that has options to pause, play, rewind, and forward a video.
The VideoView class is used to display videos in an Android app. To add media controls to the view, we can use the MediaController class which adds the media controls the UI such as play, pause, rewind, seek, and forward.
Step 1
When we need to play videos in an app, we must set app permissions to use a video from the internet.
Add the internet permission in the manifest because our code will play videos from a URL. Now, let us see our mainifest.xml file.
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- package="yourdomain.videoviewexample">
-
- <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"
- tools:ignore="GoogleAppIndexingWarning">
- <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>
Step 2
Define a video view in activity_main.xml and a text that will show the buffering state when there is buffering. We will notify users through this textview. Now, let's replace the code of our file with the one shown below.
- <android.support.constraint.ConstraintLayout
- 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="0dp"
- android:layout_height="0dp"
- android:layout_margin="8dp"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintDimensionRatio="4:3"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent"/>
-
- <TextView
- android:id="@+id/buffering_textview"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_margin="8dp"
- android:text="@string/buffering_string"
- android:textSize="18sp"
- android:textStyle="bold"
- android:textColor="@android:color/white"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent"/>
-
- </android.support.constraint.ConstraintLayout>
Here, we are using ConstraintLayot but you can use RelativeLayout as well. Now, find these views in an activity called MainActivity.java
Step 3
In our MainActivity.java, find the views in onCreate() method as shown below and intialize the videoview.
Now, you need to implement all methods for activities like onStart(), onPause(), onStop(). These methods will be executed when the Start, Pause, and Stop buttons are clicked on the controller.
The getMedia() method returns the URI of the video. If you want to play form local storage then put a file in the raw directory and write the below code.
- return Uri.parse("android.resource://" + getPackageName() +
- "/raw/" + mediaName);
Now, path here is a raw directory; simply put your media file name instead of mediaName like samplVideo.mp4 and something else.
When you play a video in an app on your phone, the Internet might be slow and buffering may occur.
The last thing we want to do is, on the onCompletion() method, we show a toast when the video is finished playing.
Output
Below screenshots are from my phone.
First, it shows buffering because it takes some time for the URL to load on VideoView.
Now here, the player is in the initialization state. Let us see the second one to visualize the video with media controls.
Now after completion, we are showing a toast message. See the completed state in the below picture.
Conclusion
In this article, we have learned how to play videos in an Android app directly from the internet.