如何从assets文件夹或raw文件夹在android中播放视频?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3028717/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to play videos in android from assets folder or raw folder?
提问by Abhishek Talwar
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 示例,并尝试使用 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();
}
});
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.
似乎没有其他方法可以在启动时将文件复制到 SD 卡,这似乎是在浪费时间和内存。
回答by Sherif elKhatib
## Perfectly Working since 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();
回答by Bishoy Fakhry
String UrlPath="android.resource://"+getPackageName()+"/"+R.raw.ur file name;
videocontainer.setVideoURI(Uri.parse(UrlPath));
videocontainer.start();
where videocontainer of type videoview.
其中视频视图类型的视频容器。
回答by Jay
Try:
尝试:
AssetFileDescriptor afd = getAssets().openFd(fileName);
player.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(), afd.getLength());
回答by haotang
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>
回答by Sephy
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 reportsabout 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.
如果我没记错的话,我在从资产文件夹加载内容但使用数据库时遇到了同样的问题。您的资产文件夹中的内容似乎可以有 2 个统计信息:压缩与否。
如果是压缩的,那么你可以有 1 个月的内存来解压缩它,否则你会得到这种异常。有几个错误报告关于这一点,因为文档不清楚。因此,如果您仍想使用您的格式,您必须使用未压缩的版本,或者为您的文件提供 .mp3 或 .png 等扩展名。我知道这有点疯狂,但我加载了一个带有 .mp3 扩展名的数据库,它工作得很好。另一种解决方案是使用特殊选项打包您的应用程序,以告诉它不要压缩某些扩展名。但是随后您需要手动构建您的应用程序并添加“zip -0”选项。
未压缩资源的优势在于,应用程序发布之前的 zip-align 阶段将正确对齐数据,以便在加载到内存中时可以直接映射。
So, solutions :
所以,解决方案:
- change the extension of the file to .mp3 or .png and see if it works
- build your app manually and use the zip-0 option
- 将文件的扩展名更改为 .mp3 或 .png 并查看它是否有效
- 手动构建您的应用程序并使用 zip-0 选项
回答by NicoMinsk
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 :
如果它正常工作,您可以在此处找到从 Raw 复制到 SDcard 的方法:
回答by Nabil Gardon
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
Personnaly Android 找到了资源,但暂时无法播放。我的文件是 .m4v
回答by Jordan Réjaud
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();
回答by hwrdprkns
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();
MediaPlayer mp = MediaPlayer.create(context, R.raw.sound_file_1); mp.start();
But I still recommend reading the information at the link.
但我仍然建议阅读链接中的信息。
回答by Khajan
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 three argument constructor.Then when
onPreared
,mediaplayer.start();
使用
MediaPlayer
API 和示例代码。将媒体文件放在
raw
文件夹中。获取
file descriptor
到文件。mediaplayer.setDataSource(fd,offset,length);
- 它是一个三参数构造函数。那么当
onPreared
,mediaplayer.start();