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.

Update

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();
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
final MediaPlayer player = new MediaPlayer();
player.setDisplay(holder);

player.setDataSource(getAssets().openFd(fileName).getFileDescriptor());
player.prepareAsync();
player.setOnPreparedListener(new OnPreparedListener() {

   @Override
   public void onPrepared(MediaPlayer mp) {
      mp.start();
   }
});

Now I get a the following exception:

java.io.FileNotFoundException: 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 ##

getWindow().setFormat(PixelFormat.TRANSLUCENT);
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
videoHolder.setVideoURI(video);
setContentView(videoHolder);
videoHolder.start();

## 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;
videocontainer.setVideoURI(Uri.parse(UrlPath));
videocontainer.start();

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

Try:

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.

PlayVideoActivity.java:

public class PlayVideoActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_play_video);

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

activity_play_video.xml:

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center" >

    <VideoView
        android:id="@+id/video_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </VideoView>

</LinearLayout>

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.

Comments
  • I think the issue is because mp4 is a compressed format. Check my answer for details
  • Here explanation about 'raw' floder stackoverflow.com/questions/11356601/…
  • 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, R.raw.video) 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