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

半世苍凉 提交于 2019-11-27 13:39:55

问题


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


来源:https://stackoverflow.com/questions/8269353/how-to-load-videos-from-assets-folder-to-play-them-with-videoview

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!