如何在android中播放assets文件夹或raw文件夹中的视频?
我正在尝试在 Android 模拟器中播放视频 我的资源文件夹和原始文件夹中都有视频 但经过一些研究后我仍然无法在模拟器中播放视频 我正在开发 android 2.1 我的视频格式是 mp4 所以我认为这不应该是一个问题 谁能给我一个示例代码,以便我能更理解一点?
问题是我需要显示视频的 VideoView 将仅使用 URI 或文件路径来指向视频。
如果我将视频保存在原始或资产文件夹中,我只能获取输入流或文件描述符,并且似乎没有任何东西可以用于初始化 VideoView。
更新
我仔细研究了 MediaPlayer 示例,并尝试使用资产文件的 FileDescriptor 启动 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();
}
});
现在我得到以下异常:
java.io.FileNotFoundException: This file can not be opened as a file descriptor; it is probably compressed
似乎没有其他然后在启动时将文件复制到 SD 卡,这似乎浪费时间和内存。
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?
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.
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.
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:
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
## 自 Android 1.6 起完美运行 ##< /a>
然后
## 查看完整教程##
## Perfectly Working since Android 1.6 ##
And then
## Check complete tutorial ##
其中 videocontainer 类型为 videoview。
where videocontainer of type videoview.
尝试:
Try:
PlayVideoActivity.java:
activity_play_video.xml:
PlayVideoActivity.java:
activity_play_video.xml:
如果我没记错的话,当我从资产文件夹加载内容但使用数据库时,我遇到了同样的问题。看来你的资产文件夹中的东西可以有 2 个统计信息:压缩或未压缩。
如果它是压缩的,那么你需要1 Mo的内存来解压缩它,否则你会得到这种异常。由于文档不清楚,因此有多个错误报告 。因此,如果您仍然想使用您的格式,则必须使用未压缩的版本,或者为文件提供 .mp3 或 .png 等扩展名。我知道这有点疯狂,但我加载了一个扩展名为 .mp3 的数据库,它工作得很好。另一种解决方案是使用特殊选项来打包您的应用程序,以告诉它不要压缩某些扩展名。但随后您需要手动构建应用程序并添加“zip -0”选项。
未压缩资产的优点是,发布应用程序之前的 zip-align 阶段将正确对齐数据,以便在加载到内存中时可以直接映射数据。
因此,解决方案:
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.
So, solutions :
您是否尝试过手动将视频放在 SDCard 上并尝试播放 SDCard 上的视频存储?
如果它有效,您可以在这里找到从 Raw 复制到 SDcard 的方法:
android-将原始文件复制到 sdcard-video-mp4。
Did you try to put manually Video on SDCard and try to play video store on SDCard ?
If it's working you can find the way to copy from Raw to SDcard here :
android-copy-rawfile-to-sdcard-video-mp4.
我找到了:
Android 个人找到了该资源,但暂时无法播放。我的文件是 .m4v
I found it :
Personnaly Android found the resource, but can't play it for now. My file is a .m4v
https://gist.github.com/jrejaud/b5eb1b013c27a1f3ae5f
https://gist.github.com/jrejaud/b5eb1b013c27a1f3ae5f
我认为你需要看看这个 - 它应该有你想要的一切。
编辑:如果您不想查看链接 - 这几乎概括了您想要的内容。
但我仍然建议阅读链接中的信息。
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.
But I still recommend reading the information at the link.
听起来你可能和我有同样的问题。不使用 MP4,可以使用 3GPP 吗?我想我使用了 HandBrake 或其他东西作为视频转换器...您只需要确保您有正确的编码器,例如 H.264x 或其他东西。抱歉有点含糊,已经有一段时间了。另外,如果可能的话,不用再担心 android 2.1,而且,有些东西在模拟器中无法工作。因此,如果它可以在很多设备上运行,那么就假设它可以
在这里运行(尤其是对于不同的制造商),您可以阅读我的问题以及我如何解决该问题(很长一段时间后没有人给出答案)。我在这里更详细地解释了:
Android 媒体播放器不工作
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)
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
在
fileName
中,您必须输入文件的相对路径(不带/asset
)例如:
In the
fileName
you must put the relative path to the file (without/asset
)for example:
使用
MediaPlayer
API 和示例代码。将媒体文件放入
raw
文件夹中。获取文件的
文件描述符
。mediaplayer.setDataSource(fd,offset,length);
- 它是一个三参数构造函数。
然后当
onPreared
时,mediaplayer.start();
Use the
MediaPlayer
API and the sample code.Put the media file in
raw
folder.Get the
file descriptor
to the file.mediaplayer.setDataSource(fd,offset,length);
- its a threeargument constructor.
Then when
onPreared
,mediaplayer.start();
主代码
XML
MainCode
XML