如何在android中从资产文件夹或原始文件夹播放视频?

时间:2022-07-09 00:28:05

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?

我试图在android模拟器播放视频我的视频在我的资产文件夹以及原始文件夹但仍在做一些研究我在模拟器不能播放视频我在android 2.1视频格式是mp4所以我认为不应该成为一个问题有人能给我一个例子代码,这样我可以了解更多吗?

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将只使用URI或文件路径指向视频。

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.

如果我将视频保存在raw或assets文件夹中,我只能得到一个输入流或一个文件描述符,似乎没有什么可以用来初始化VideoView。

Update

更新

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:

我仔细查看了MediaPlayer示例,并尝试使用文件描述符来启动一个MediaPlayer,如下面的代码所示:

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

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.

似乎没有其他方法可以在启动时将文件复制到sdcard,这似乎是浪费时间和内存。

13 个解决方案

#1


109  

## Perfectly Working since Android 1.6 ##

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

## Check complete tutorial ##

检查完整的教程#

#2


11  

Try:

试一试:

AssetFileDescriptor afd = getAssets().openFd(fileName);
player.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(), afd.getLength());

#3


11  

String UrlPath="android.resource://"+getPackageName()+"/"+R.raw.ur file name;
videocontainer.setVideoURI(Uri.parse(UrlPath));
videocontainer.start();

where videocontainer of type videoview.

视频容器类型的视频视图。

#4


9  

PlayVideoActivity.java:

PlayVideoActivity.java:

public class PlayVideoActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_play_video);

        VideoView videoView = (VideoView) findViewById(R.id.video_view);
        MediaController mediaController = new MediaController(this);
        mediaController.setAnchorView(videoView);
        videoView.setMediaController(mediaController);
        videoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.documentariesandyou));
        videoView.start();
    }
}

activity_play_video.xml:

activity_play_video.xml:

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center" >

    <VideoView
        android:id="@+id/video_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </VideoView>

</LinearLayout>

#5


5  

If I remember well, I had the same kind of issue when loading stuff from the asset folder but with a database. It seems that the stuff in your asset folder can have 2 stats : compressed or not.
If it is compressed, then you are allowed 1 Mo of memory to uncompress it, otherwise you will get this kind of exception. There are several bug reports about that because the documentation is not clear. So if you still want to to use your format, you have to either use an uncompressed version, or give an extension like .mp3 or .png to your file. I know it's a bit crazy but I load a database with a .mp3 extension and it works perfectly fine. This other solution is to package your application with a special option to tell it not to compress certain extension. But then you need to build your app manually and add "zip -0" option.
The advantage of an uncompressed assest is that the phase of zip-align before publication of an application will align the data correctly so that when loaded in memory it can be directly mapped.

如果我记得很清楚,当从资产文件夹加载数据时,我遇到了同样的问题,但是使用数据库。似乎您的资产文件夹中的内容可以有两个属性:压缩或不压缩。如果它被压缩了,那么你可以用1摩尔的内存解压它,否则你会得到这种异常。关于这个问题有几个bug报告,因为文档不清楚。所以如果你还想使用你的格式,你必须使用一个未压缩的版本,或者给你的文件一个扩展名,比如。mp3或者。png。我知道这有点疯狂,但我加载了一个扩展名为.mp3的数据库,它运行得非常好。另一种解决方案是为应用程序打包一个特殊选项,告诉它不要压缩某些扩展。但是,您需要手工构建应用程序并添加“zip -0”选项。未压缩的assest的优点是,发布应用程序之前的zip-align阶段将正确地对齐数据,以便在加载到内存中时可以直接映射数据。

So, solutions :

  • change the extension of the file to .mp3 or .png and see if it works
  • 将文件的扩展名更改为.mp3或.png,看看是否有效
  • build your app manually and use the zip-0 option
  • 手动构建应用程序并使用zip-0选项

#6


4  

Did you try to put manually Video on SDCard and try to play video store on SDCard ?

你有没有尝试在SDCard上手动播放视频,并尝试在SDCard上播放视频存储?

If it's working you can find the way to copy from Raw to SDcard here :

如果它是有效的,你可以找到方法从原始到SDcard这里:

android-copy-rawfile-to-sdcard-video-mp4.

android-copy-rawfile-to-sdcard-video-mp4。

#7


3  

I found it :

我发现了它:

Uri raw_uri = Uri.parse(<package_name>/+R.raw.<video_file_name>);

Personnaly Android found the resource, but can't play it for now. My file is a .m4v

Android个人找到了这个资源,但是现在不能运行它。我的文件是。m4v

#8


2  

I think that you need to look at this -- it should have everything you want.

我觉得你应该看看这个——它应该有你想要的一切。

EDIT: If you don't want to look at the link -- this pretty much sums up what you'd like.

编辑:如果你不想看链接的话——这差不多就是你想要的。

MediaPlayer mp = MediaPlayer.create(context, R.raw.sound_file_1); mp.start();

媒体播放器*=媒体播放器。创建(上下文,R.raw.sound_file_1);mp.start();

But I still recommend reading the information at the link.

但我仍然建议您阅读链接上的信息。

#9


2  

VideoView myVideo = (VideoView) rootView.findViewById(R.id.definition_video_view);

//Set video name (no extension)
String myVideoName = "my_video";

//Set app package
String myAppPackage = "com.myapp";

//Get video URI from raw directory
Uri myVideoUri = Uri.parse("android.resource://"+myAppPackage+"/raw/"+myVideoName);

//Set the video URI
myVideo.setVideoURI(myVideoUri);

//Play the video
myVideo.start();

https://gist.github.com/jrejaud/b5eb1b013c27a1f3ae5f

https://gist.github.com/jrejaud/b5eb1b013c27a1f3ae5f

#10


1  

It sounds maybe like you have the same issue as i do. instead of using MP4, is 3GPP possible? i think i used like HandBrake or something as the video converter... you just need to make sure you have the right encoder, like H.264x or something. sorry for being a little vague, it's been a while. Also, if it's possible, don't bother worrying about android 2.1 anymore, and also, something things just WILL NOT WORK IN EMULATOR. so if it works on a lot of devices, then just assume it works (especially with different manufacurers)

听起来好像你和我有同样的问题。不使用MP4, 3GPP可能吗?我想我用的是手刹车或者其他的视频转换器…你只需要确保你有正确的编码器,比如h。264x或者其他什么。抱歉说得有点含糊,已经有一段时间了。此外,如果可能的话,不要再担心android 2.1了,而且有些东西在模拟器中无法工作。所以,如果它在很多设备上都能工作,那就假设它能工作(特别是在不同的制造商)

here, you can read my problem and how i solved the issue (after a long time and no one had an answer). i explained in a lot more detail here: android media player not working

在这里,你可以阅读我的问题以及我是如何解决这个问题的(很长一段时间后,没有人知道答案)。我在这里解释了更多细节:android媒体播放器不工作

#11


1  

In the fileName you must put the relative path to the file (without /asset) for example:

在文件名中,必须将相对路径放入文件(没有/资产),例如:

player.setDataSource(
    getAssets().openFd(**"media/video.mp4"**).getFileDescriptor()
);

#12


1  

  1. Use the MediaPlayer API and the sample code.

    使用MediaPlayer API和示例代码。

  2. Put the media file in raw folder.

    将媒体文件放在原始文件夹中。

  3. Get the file descriptor to the file.

    获取文件描述符。

  4. mediaplayer.setDataSource(fd,offset,length); - its a three argument constructor.

    mediaplayer.setDataSource(fd、抵消、长度);-它是一个三参数构造函数。

  5. Then when onPreared , mediaplayer.start();

    然后,当插入mediaplayer.start();

#13


0  

MainCode

MainCode

Uri raw_uri=Uri.parse("android.resource://<package_name>/+R.raw.<video_file_name>);

myVideoView=(VideoView)findViewbyID(R.idV.Video_view);

myVideoView.setVideoURI(raw_uri);
myVideoView.setMediaController(new MediaController(this));
myVideoView.start();
myVideoView.requestFocus();

XML

XML

<?xml version="1.0" encoding="utf-8" ?>

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
        <VideoView
            android:id="+@/Video_View"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
        />
</LinearLayout>

#1


109  

## Perfectly Working since Android 1.6 ##

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

## Check complete tutorial ##

检查完整的教程#

#2


11  

Try:

试一试:

AssetFileDescriptor afd = getAssets().openFd(fileName);
player.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(), afd.getLength());

#3


11  

String UrlPath="android.resource://"+getPackageName()+"/"+R.raw.ur file name;
videocontainer.setVideoURI(Uri.parse(UrlPath));
videocontainer.start();

where videocontainer of type videoview.

视频容器类型的视频视图。

#4


9  

PlayVideoActivity.java:

PlayVideoActivity.java:

public class PlayVideoActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_play_video);

        VideoView videoView = (VideoView) findViewById(R.id.video_view);
        MediaController mediaController = new MediaController(this);
        mediaController.setAnchorView(videoView);
        videoView.setMediaController(mediaController);
        videoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.documentariesandyou));
        videoView.start();
    }
}

activity_play_video.xml:

activity_play_video.xml:

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center" >

    <VideoView
        android:id="@+id/video_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </VideoView>

</LinearLayout>

#5


5  

If I remember well, I had the same kind of issue when loading stuff from the asset folder but with a database. It seems that the stuff in your asset folder can have 2 stats : compressed or not.
If it is compressed, then you are allowed 1 Mo of memory to uncompress it, otherwise you will get this kind of exception. There are several bug reports about that because the documentation is not clear. So if you still want to to use your format, you have to either use an uncompressed version, or give an extension like .mp3 or .png to your file. I know it's a bit crazy but I load a database with a .mp3 extension and it works perfectly fine. This other solution is to package your application with a special option to tell it not to compress certain extension. But then you need to build your app manually and add "zip -0" option.
The advantage of an uncompressed assest is that the phase of zip-align before publication of an application will align the data correctly so that when loaded in memory it can be directly mapped.

如果我记得很清楚,当从资产文件夹加载数据时,我遇到了同样的问题,但是使用数据库。似乎您的资产文件夹中的内容可以有两个属性:压缩或不压缩。如果它被压缩了,那么你可以用1摩尔的内存解压它,否则你会得到这种异常。关于这个问题有几个bug报告,因为文档不清楚。所以如果你还想使用你的格式,你必须使用一个未压缩的版本,或者给你的文件一个扩展名,比如。mp3或者。png。我知道这有点疯狂,但我加载了一个扩展名为.mp3的数据库,它运行得非常好。另一种解决方案是为应用程序打包一个特殊选项,告诉它不要压缩某些扩展。但是,您需要手工构建应用程序并添加“zip -0”选项。未压缩的assest的优点是,发布应用程序之前的zip-align阶段将正确地对齐数据,以便在加载到内存中时可以直接映射数据。

So, solutions :

  • change the extension of the file to .mp3 or .png and see if it works
  • 将文件的扩展名更改为.mp3或.png,看看是否有效
  • build your app manually and use the zip-0 option
  • 手动构建应用程序并使用zip-0选项

#6


4  

Did you try to put manually Video on SDCard and try to play video store on SDCard ?

你有没有尝试在SDCard上手动播放视频,并尝试在SDCard上播放视频存储?

If it's working you can find the way to copy from Raw to SDcard here :

如果它是有效的,你可以找到方法从原始到SDcard这里:

android-copy-rawfile-to-sdcard-video-mp4.

android-copy-rawfile-to-sdcard-video-mp4。

#7


3  

I found it :

我发现了它:

Uri raw_uri = Uri.parse(<package_name>/+R.raw.<video_file_name>);

Personnaly Android found the resource, but can't play it for now. My file is a .m4v

Android个人找到了这个资源,但是现在不能运行它。我的文件是。m4v

#8


2  

I think that you need to look at this -- it should have everything you want.

我觉得你应该看看这个——它应该有你想要的一切。

EDIT: If you don't want to look at the link -- this pretty much sums up what you'd like.

编辑:如果你不想看链接的话——这差不多就是你想要的。

MediaPlayer mp = MediaPlayer.create(context, R.raw.sound_file_1); mp.start();

媒体播放器*=媒体播放器。创建(上下文,R.raw.sound_file_1);mp.start();

But I still recommend reading the information at the link.

但我仍然建议您阅读链接上的信息。

#9


2  

VideoView myVideo = (VideoView) rootView.findViewById(R.id.definition_video_view);

//Set video name (no extension)
String myVideoName = "my_video";

//Set app package
String myAppPackage = "com.myapp";

//Get video URI from raw directory
Uri myVideoUri = Uri.parse("android.resource://"+myAppPackage+"/raw/"+myVideoName);

//Set the video URI
myVideo.setVideoURI(myVideoUri);

//Play the video
myVideo.start();

https://gist.github.com/jrejaud/b5eb1b013c27a1f3ae5f

https://gist.github.com/jrejaud/b5eb1b013c27a1f3ae5f

#10


1  

It sounds maybe like you have the same issue as i do. instead of using MP4, is 3GPP possible? i think i used like HandBrake or something as the video converter... you just need to make sure you have the right encoder, like H.264x or something. sorry for being a little vague, it's been a while. Also, if it's possible, don't bother worrying about android 2.1 anymore, and also, something things just WILL NOT WORK IN EMULATOR. so if it works on a lot of devices, then just assume it works (especially with different manufacurers)

听起来好像你和我有同样的问题。不使用MP4, 3GPP可能吗?我想我用的是手刹车或者其他的视频转换器…你只需要确保你有正确的编码器,比如h。264x或者其他什么。抱歉说得有点含糊,已经有一段时间了。此外,如果可能的话,不要再担心android 2.1了,而且有些东西在模拟器中无法工作。所以,如果它在很多设备上都能工作,那就假设它能工作(特别是在不同的制造商)

here, you can read my problem and how i solved the issue (after a long time and no one had an answer). i explained in a lot more detail here: android media player not working

在这里,你可以阅读我的问题以及我是如何解决这个问题的(很长一段时间后,没有人知道答案)。我在这里解释了更多细节:android媒体播放器不工作

#11


1  

In the fileName you must put the relative path to the file (without /asset) for example:

在文件名中,必须将相对路径放入文件(没有/资产),例如:

player.setDataSource(
    getAssets().openFd(**"media/video.mp4"**).getFileDescriptor()
);

#12


1  

  1. Use the MediaPlayer API and the sample code.

    使用MediaPlayer API和示例代码。

  2. Put the media file in raw folder.

    将媒体文件放在原始文件夹中。

  3. Get the file descriptor to the file.

    获取文件描述符。

  4. mediaplayer.setDataSource(fd,offset,length); - its a three argument constructor.

    mediaplayer.setDataSource(fd、抵消、长度);-它是一个三参数构造函数。

  5. Then when onPreared , mediaplayer.start();

    然后,当插入mediaplayer.start();

#13


0  

MainCode

MainCode

Uri raw_uri=Uri.parse("android.resource://<package_name>/+R.raw.<video_file_name>);

myVideoView=(VideoView)findViewbyID(R.idV.Video_view);

myVideoView.setVideoURI(raw_uri);
myVideoView.setMediaController(new MediaController(this));
myVideoView.start();
myVideoView.requestFocus();

XML

XML

<?xml version="1.0" encoding="utf-8" ?>

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
        <VideoView
            android:id="+@/Video_View"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
        />
</LinearLayout>