如何从资产的文件夹或文件夹生的android播放视频?文件夹、资产、文件、视频

2023-09-11 12:28:40 作者:亡

我想打在Android模拟器的视频 我在我的资产文件夹中的视频以及原始文件夹 但在做了一些研究之后,我还是不能在我的模拟器玩视频 我工作在Android 2.1 我的视频格式为MP4,所以我不认为这应该是一个问题 任何人都可以随便给我一个例子code,这样我可以了解多一点?

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?

的问题是,我需要显示视频的VideoView将只需要一个URI或文件路径指向视频。

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.

如果我保存视频在原料或资产夹我只能得到一个输入流或一个文件描述符,似乎没有任何的可用于初始化VideoView

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.

更新

我仔细看一下MediaPlayer的例子​​,试图启动一个的FileDescriptor的资产文件的MediaPlayer如下面的code:

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

这似乎也没有别的办法,然后将文件复制到启动时的SD卡,并且似乎是在浪费时间和内存。

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.

推荐答案

的 ##,因为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();

##查阅完整的教程##