NullPointerException
NullPointerException

Reputation: 37579

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

Upvotes: 28

Views: 38061

Answers (3)

Jorgesys
Jorgesys

Reputation: 126445

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();

Upvotes: 30

Caner
Caner

Reputation: 59148

EDITED

Try:

this.setContentView(R.layout.videoview);        
mVideoView = (VideoView) this.findViewById(R.id.surface_view);        
SurfaceHolder holder = mVideoView.getHolder();
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
MediaPlayer player = new MediaPlayer();
player.setDisplay(holder);        
AssetFileDescriptor afd;
try {
    afd = getAssets().openFd("v.mp4");        
    player.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(), afd.getLength());
    player.prepareAsync();
    player.setOnPreparedListener(new OnPreparedListener() {

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

} catch (Exception e) { e.printStackTrace();}   

Upvotes: 6

js-
js-

Reputation: 1632

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);

Upvotes: 35

Related Questions