- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to play videos in Android from assets folder or raw folder?
This example demonstrates how do I play videos in Android from the assets folder or raw folder in android.
Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.
Step 2 − Add the following code to res/layout/activity_main.xml.
<?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:gravity="center" android:orientation="vertical" android:padding="8dp" tools:context=".MainActivity"> <VideoView android:id="@+id/videoView" android:layout_width="match_parent" android:layout_height="match_parent"> </VideoView> </LinearLayout>
Step 3 − Create a new android resource folder(raw) and copy-paste your video file in that folder.
Step 4 − Add the following code to src/MainActivity.java
import androidx.appcompat.app.AppCompatActivity; import android.net.Uri; import android.os.Bundle; import android.widget.MediaController; import android.widget.VideoView; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); VideoView videoView = findViewById(R.id.videoView); MediaController mediaController = new MediaController(this); mediaController.setAnchorView(videoView); videoView.setMediaController(mediaController); videoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.videoplayback)); videoView.start(); } }
Step 5 − Add the following code to androidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="app.com.sample"> <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"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
Let's try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from the android studio, open one of your project's activity files and click the Run icon from the toolbar. Select your mobile device as an option and then check your mobile device which will display your default screen −
- Related Articles
- How to write files to the assets folder in android using Kotlin?
- How to play videos in Android TextureView?
- How to play audio file from the assets directory in Android?
- How to write files to asset folder in android?
- How to play videos in Android TextureView using Kotlin?
- How to load classes at runtime from a folder or Java package
- How to play YouTube videos in my Android application using Kotlin?
- How to Copy files or Folder without overwriting existing files?
- How to delete a file from the public folder in Laravel?
- How to copy files from one folder to another using Python?
- Create Directory or Folder with C/C++ Program
- How to read multiple text files from a folder in Python?(Tkinter)
- How to get the directories (only) from a folder using Java?
- How to copy certain files from one folder to another using Python?
- How to move a file from one folder to another using Python?
