How to Play Video from URL and Local Resource using VideoView in Android
In this blog post, you will learn how to create an Android app that plays videos from both an online URL and a local resource using VideoView. The post includes detailed explanations, project structure guidance, and beautifully highlighted code samples.
Project Structure Setup:
- Start a new project in Android Studio.
- To add local videos, create a directory: res/raw. If it doesn’t exist, right-click res → New → Android Resource Directory → choose raw.
- Add your local video file (e.g.,
cartoon.mp4
) inside thisraw
folder.
1. XML Layout File: activity_video_view.xml
The XML defines a VideoView and two buttons, each responsible for playing video from URL and local resources.
<?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:id="@+id/main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="16dp" tools:context=".VideoViewActivity"> <VideoView android:id="@+id/videoView" android:layout_width="match_parent" android:layout_height="300dp" android:layout_marginBottom="16dp" /> <Button android:id="@+id/buttonPlayUrl" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="▶ Play Video from URL" android:textSize="18sp" android:background="@android:color/holo_blue_light" android:textColor="@android:color/white" android:layout_marginBottom="10dp"/> <Button android:id="@+id/buttonPlayLocal" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="▶ Play Video from Local" android:textSize="18sp" android:background="@android:color/holo_green_light" android:textColor="@android:color/white"/> </LinearLayout>
2. Java Activity File: VideoViewActivity.java
This activity handles playback from both online and local sources, showing simple event listeners for each button.
package com.vipul.p6; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.MediaController; import android.widget.VideoView; import androidx.appcompat.app.AppCompatActivity; public class VideoViewActivity extends AppCompatActivity { VideoView videoView; Button buttonPlayUrl, buttonPlayLocal; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_video_view); videoView = findViewById(R.id.videoView); buttonPlayUrl = findViewById(R.id.buttonPlayUrl); buttonPlayLocal = findViewById(R.id.buttonPlayLocal); buttonPlayUrl.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Uri videoUri = Uri.parse("https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ElephantsDream.mp4"); videoView.setVideoURI(videoUri); videoView.start(); } }); buttonPlayLocal.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Uri localUri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.cartoon); videoView.setVideoURI(localUri); videoView.start(); } }); MediaController mediaController = new MediaController(this); videoView.setMediaController(mediaController); mediaController.setAnchorView(videoView); } }
✅ Don’t forget to add permission in AndroidManifest.xml
:
<uses-permission android:name="android.permission.INTERNET" />
That's it! 🎉 Try this code into your project, and you're ready to play videos from both URL and local files in Android.
0 Comments
If you have any doubts, Please let me know