How to load videos from assets folder? (to play th

2019-01-22 19:03发布

问题:

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

回答1:

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


回答2:

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


回答3:

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