Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load videos from assets folder? (to play them with VideoView)

I need to open a mp4 video from the assets folder and play it with VideoView.

I tryed with these two options, but none of them works....

mVideoView.setVideoPath("file:///android_asset/videos.mp4"); mVideoView.requestFocus(); mVideoView.start(); 

and...

String uriPath = "file:///android_asset/videos.mp4"; Uri uri = Uri.parse(uriPath); mVideoView.setVideoURI(uri); mVideoView.requestFocus(); mVideoView.start(); 

These options didn't works, but if I try to open the video from the SDCARD it works perfectly, then, the problem is when I'm trying to load the video from the assets folder.

What I'm doing wrong?

thanks

like image 337
NullPointerException Avatar asked Nov 25 '11 12:11

NullPointerException


People also ask

How do I load an asset file?

Right click on the assets folder, select New >> file (myText. txt) and your text.

How do I use an assets folder?

Step 1: To create an asset folder in Android studio open your project in Android mode first as shown in the below image. Step 2: Go to the app > right-click > New > Folder > Asset Folder and create the asset folder. Step 3: Android Studio will open a dialog box. Keep all the settings default.


2 Answers

I don't know how to load videos from the assets folder. But I know how to load them from the /res/raw/ folder:

String uriPath = "android.resource://yourapplicationpackage/raw/videofilenamewithoutextension"; Uri uri = Uri.parse(uriPath); video.setVideoURI(uri); 
like image 159
js- Avatar answered Sep 19 '22 03:09

js-


Playing the .mp4 file from \assets is not possible, you must load it from \raw folder.

Example:

if you have a file called video.mp4 inside the /raw folder:

String fileName = "android.resource://"+  getPackageName() + "/raw/video"; VideoView vv = (VideoView) this.findViewById(R.id.surface); vv.setVideoURI(Uri.parse(fileName)); vv.start(); 
like image 45
Jorgesys Avatar answered Sep 22 '22 03:09

Jorgesys