How to play videos in android from assets folder or raw folder?

set video from raw folder in android
android play video from res
how to play video in android
exoplayer play from assets
how to access raw folder in android
exoplayer play video from raw folder
can t play this video android videoview
how to add video in android studio

I am trying to play a video in android emulator I have the video in my assets folder as well as the raw folder But after doing some research still i cant play video in my emulator i am working on android 2.1 My video format is mp4 so i don't think that should be a problem Could anyone just give me an example code so that i can understand a bit more?

The problem is that the VideoView that I need to display the Video will take only a URI or a File path to point to the Video.

If I save the video in the raw or assets folder I can only get an input stream or a file descriptor and it seems nothing of that can be used to initialize the VideoView.


I took a closer look at the MediaPlayer example and tried to start a MediaPlayer with a FileDescriptor to the assets files as in the code below:

SurfaceView videoView = (SurfaceView) findViewById(gettingStarted)
SurfaceHolder holder = videoView.getHolder();
final MediaPlayer player = new MediaPlayer();

player.setOnPreparedListener(new OnPreparedListener() {

   public void onPrepared(MediaPlayer mp) {

Now I get a the following exception: This file can not be opened as a file descriptor; it is probably compressed

It seems there is no other way then copying the file to the sdcard on startup and that seems like a waste of time and memory.

## Perfectly Working since Android 1.6 ##

VideoView videoHolder = new VideoView(this);
//if you want the controls to appear
videoHolder.setMediaController(new MediaController(this));
Uri video = Uri.parse("android.resource://" + getPackageName() + "/" 
+ R.raw.your_raw_file); //do not add any extension
//if your file is named sherif.mp4 and placed in /raw
//use R.raw.sherif

## Check complete tutorial ##

How to play videos in android from assets folder or raw folder , ## Perfectly Working since Android 1.6 ## getWindow().setFormat(PixelFormat.​TRANSLUCENT); VideoView videoHolder = new  Use the MediaPlayer API and the sample code. Put the media file in raw folder. Get the file descriptor to the file. mediaplayer.setDataSource(fd,offset,length); - its a three argument constructor.

String UrlPath="android.resource://"+getPackageName()+"/"+R.raw.ur file name;

where videocontainer of type videoview.

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  Most of the people tell me to move the video in the res/raw folder because most of them have no idea how to load from the assets folder. In this case the second anwser would work. Of course this isn't an option if you develop an ANE for Air Applications like me. All the other atemps seems to be based on file://android_assets and are sketchy at


AssetFileDescriptor afd = getAssets().openFd(fileName);
player.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(), afd.getLength());

How to play videos in android from assets folder or raw , folder or raw folder? player.setDataSource( getAssets().openFd(**"media/video​.mp4"**).getFileDescriptor() );. In the fileName you must put the relative path to  After some time I am back this time with a simple programm for playing video file from raw folder. Create a folder raw in res folder & then save video in that folder. next follow below code ate the end you will see video playing.. main.xml.

public class PlayVideoActivity extends Activity {

    public void onCreate(Bundle savedInstanceState) {

        VideoView videoView = (VideoView) findViewById(;
        MediaController mediaController = new MediaController(this);
        videoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.documentariesandyou));


<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android=""
    android:gravity="center" >

        android:layout_height="match_parent" >


LeslieCorrea/Android-Playing-Video-From-Assets: This , This repository explains how to play a video from android asset folder. Description. By default android VideoView plays local videos placed in raw folder​, but for  Hello, I am trying to play an mp4 file using exoplayer. I have the mp4 file on my android "raw" folder and it can be moved to "assets" folder if needed. I have tried modifying the demo app - "SampleChooserAcitivity" as below: sampleAdapt

Playing mp4 video, Hello, I am trying to play an mp4 file using exoplayer. I have the mp4 file on my android "raw" folder and it can be moved to "assets" folder if  Assets and resources are files on your development machine. They are not files on the device. For assets, use open() on AssetManager to get an InputStream on your asset.

android.widget.VideoView java code examples, How to play videos in android from assets folder or raw folder? getWindow().​setFormat(PixelFormat.TRANSLUCENT); VideoView videoHolder = new  Finally, line 70 feeds this ID to the openRawResource() method, which will return a InputStream from a file located at the res/raw folder. At the else block, the Assets folder is being opened, by first calling the getAssets() method to return a handle to the Assets folder and right after that, the open() method is called, passing the fileName as the parameter.

How to play video from raw folder with Android device?, . It must be in a supported format (3gp, wmv, mp4 ) and named with lower case, numerics, underscores and dots in its filename likewise:video_file. What am I doing wrong? Is there a better way to play audio files from the assets directory? Follow-up question: Is there a difference between keeping audio files in the assets directory and keeping them in the res/raw directory? Besides the fact that they don't get ids if they are in the assets directory.

Load mp3 Resource from Raw Folder in Android, [Android] How to access media data in assets and res/raw folder. To make custom media player, I tried to use MediaPlayer. But when I set datasource, it could not work well. I placed video files in assets/ or res/raw. I didn't  1. Start Android Studio on your computer. 2. Open your project like i do in below example screenshot. 3. Right click on res folder. Then select New -> Folder -> Assets folder. 4. Now check on Change Folder Location and click on finish button to add assets folder inside your project in android studio.

  • I think the issue is because mp4 is a compressed format. Check my answer for details
  • Here explanation about 'raw' floder…
  • IMPORTANT: To use the R.raw path, you must put your video in the "res/raw" folder and not under "assets".
  • What is "R" here? aka R.raw? What the type of R variable or this is class?
  • its not working on micromax canvas 4 , instead its giving StringIndexOutofBound exception
  • Also - only works for video formats supported by the native Android
  • if you don't call getWindow().setFormat(PixelFormat.TRANSLUCENT);, make sure you call videoView.setZOrderOnTop(true); on your videoview before playing the video, otherwise it will appear behind your views and you will be very confused; if you get Error(-21851621) then you need to recode your H264 files (H264 for example worked for me with LEAWO Total Media Converter)
  • The AssetFileDescriptor version of setDataSource was added in API level 24; OP is asking for something that will work on android 2.3.
  • i tried this but in jelly beans it doesn't work. I don't know if its only because of my phone. But it works on newer devices.
  • hey thanks dude but i have looked at this page for like million times and have used this line of code. It does play audio files but not the video and i think there is something related with videoview and assets or raw folder. If you have any idea in your head please feel free to share or a piece of code.
  • If I call MediaPlayer.create(contect, I get the same exception, inside the MediaPlayer class, as if I prepare the Mediaplayer myself with a file descriptor.
  • ok thnx i will try this but can you please provide me with some sample code that would be very helpfull. thanks again