Hello, coders. In this load and play video from URL android studio tutorial example, we will learn to play or load video from URL or server.
You can load video from URL in android Videoview programmatically.
First, check the output of this load and play video from URL Android and then we will implement it step by step.
Wait for one minute
Here, we will load or play video from URL. If you wish to load video from Android studio’s raw directory, then check here: Videoview android studio example tutorial.
Download Source Code for load and play video from url android
[sociallocker] Download Demo [/sociallocker]
Try Cake Application
Wish your dear ones happy birthday with special birthday cake which contains his/her name and photo on cake.
Visit app here : Photo and Name on Birthday Cake
Step 1: Create a new project in the android studio.
Step 2: Updating AndroidManifest.xml file
add internet permission between <manifest>….</manifest> tag.
<uses-permission android:name="android.permission.INTERNET" />
Note: If you are targeting SDK version above 22 (Above Lollipop) then you need to ask a user for granting runtime permissions. Check marshmallow runtime permission for more information.
Step 3: Updating activity_main.xml file
Add below code in activity_main.xml file
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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:orientation="vertical"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:id="@+id/btnonce" android:text="Once"/> <Button android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:id="@+id/btnconti" android:text="continuously"/> <Button android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:id="@+id/btnstop" android:text="Stop"/> <Button android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:id="@+id/btnplay" android:text="play"/> </LinearLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <VideoView android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentBottom="true" android:layout_alignParentEnd="true" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_alignParentStart="true" android:layout_alignParentTop="true" android:id="@+id/vv"/> <ProgressBar android:layout_width="200dp" android:layout_height="200dp" android:id="@+id/progrss" android:visibility="gone" android:layout_centerInParent="true"/> </RelativeLayout> </LinearLayout>
Step 4: Updating MainActivity.java class :
Add below code to MainActivity.java
import android.media.MediaPlayer; import android.net.Uri; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.MediaController; import android.widget.ProgressBar; import android.widget.VideoView; public class MainActivity extends AppCompatActivity { private Button btnonce, btncontinuously, btnstop, btnplay; private VideoView vv; private MediaController mediacontroller; private Uri uri; private boolean isContinuously = false; private ProgressBar progressBar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); progressBar = (ProgressBar) findViewById(R.id.progrss); btnonce = (Button) findViewById(R.id.btnonce); btncontinuously = (Button) findViewById(R.id.btnconti); btnstop = (Button) findViewById(R.id.btnstop); btnplay = (Button) findViewById(R.id.btnplay); vv = (VideoView) findViewById(R.id.vv); mediacontroller = new MediaController(this); mediacontroller.setAnchorView(vv); String uriPath = "http://www.demonuts.com/Demonuts/smallvideo.mp4"; //update package name uri = Uri.parse(uriPath); vv.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) { if(isContinuously){ vv.start(); } } }); btnstop.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { vv.pause(); } }); btnplay.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { vv.start(); } }); btnonce.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { isContinuously = false; progressBar.setVisibility(View.VISIBLE); vv.setMediaController(mediacontroller); vv.setVideoURI(uri); vv.requestFocus(); vv.start(); } }); btncontinuously.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { isContinuously = true; progressBar.setVisibility(View.VISIBLE); vv.setMediaController(mediacontroller); vv.setVideoURI(uri); vv.requestFocus(); vv.start(); } }); vv.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { // Close the progress bar and play the video public void onPrepared(MediaPlayer mp) { progressBar.setVisibility(View.GONE); } }); } }
Step 5: Description of MainActivity.java
- Below code
mediacontroller = new MediaController(this); mediacontroller.setAnchorView(vv);
- will set mediacontrollers(previous, pause, play, next buttons) to videoview.
- Below code
String uriPath = "http://www.demonuts.com/Demonuts/smallvideo.mp4"; //update server url uri = Uri.parse(uriPath);
- Here you need to update your server URL to the video.
-
The Following code will run when a video has finished it’s run.
vv.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) { if(isContinuously){ vv.start(); } } });
- As you can see we are using boolean isContinuously to detect which button is pressed, Either “ONCE” or “Continuously .“
- We are managing isContinuously and starting a video on both button’s click method as below.
btnonce.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { isContinuously = false; progressBar.setVisibility(View.VISIBLE); vv.setMediaController(mediacontroller); vv.setVideoURI(uri); vv.requestFocus(); vv.start(); } }); btncontinuously.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { isContinuously = true; progressBar.setVisibility(View.VISIBLE); vv.setMediaController(mediacontroller); vv.setVideoURI(uri); vv.requestFocus(); vv.start(); } });
We will remove progressBar when a video is prepared to run. See below code
vv.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { // Close the progress bar and play the video public void onPrepared(MediaPlayer mp) { progressBar.setVisibility(View.GONE); } });
Kotlin Version
Kotlin Version : Android Kotlin Play Video From URL
So all for this play video url android studio example tutorial.
If you have any query regarding this tutorial, then ask in the comment section.
Thank you for your time, keep visiting for more tutorials.