Android 我想播放我的资产或原始文件夹中的视频
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3746361/
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
I want to play a video from my assets or raw folder
提问by James
I want to play a video from my assets or raw folder in my app in Android
using VideoView
I am getting the error as video cannot be played
please anyone give me a solution.
我想在我的 Android 应用程序中播放我的资产或原始文件夹中的视频,我VideoView
收到错误,因为视频无法播放,请任何人给我一个解决方案。
Here is the code I used
这是我使用的代码
VideoView vd = (VideoView)findViewById(R.id.Video);
Uri uri = Uri.parse("android.resource:" + R.raw.video);
MediaController mc = new MediaController(this);
vd.setMediaController(mc);
vd.setVideoURI(uri);
vd.start();
回答by Darren Hicks
A few things to note:
需要注意的几点:
- You must copy the video into your project's res/rawfolder.
- It must be in a supported format (3gp, wmv, mp4 ) and named with lower case, numerics, underscores and dots in its filename: my_video_file.mp4
- When you work with this resource in code, you will reference through the generated R statics - it will have the file extension removed: R.raw.my_video_file
- The Activity class has a helper method getPackageName() which can be used by your code when constructing the correct URI to your video.
- 您必须将视频复制到项目的res/raw文件夹中。
- 它必须采用受支持的格式(3gp、wmv、mp4)并在其文件名中以小写、数字、下划线和点命名:my_video_file.mp4
- 当您在代码中使用此资源时,您将通过生成的 R 静态引用进行引用 - 它将删除文件扩展名:R.raw.my_video_file
- Activity 类有一个辅助方法 getPackageName(),您的代码可以在为您的视频构建正确的 URI 时使用该方法。
VideoView vv = (VideoView)this.findViewById(R.id.videoView)
String uri = "android.resource://" + getPackageName() + "/" + R.raw.my_video_file;
vv.setVideoURI(Uri.parse(uri));
vv.start();
There is more information on this here.
这里有更多关于这方面的信息。
回答by Paul Burke
You must include the package name in the uri:
您必须在 uri 中包含包名称:
Uri uri = Uri.parse("android.resource://[package]/raw/video")
or
或者
Uri uri = Uri.parse("android.resource://[package]/"+R.raw.video);
Also, check out these examples.
另外,请查看这些示例。
回答by Marky0
There are so many ways to go wrong with VideoView ! Mainly because the logcat gives you no help, always giving error UNKNOWN.
VideoView 有很多出错的地方!主要是因为 logcat 没有给你任何帮助,总是给出错误 UNKNOWN。
I found this link was by far the best way to get started... A complete description so you can't go wrong. Thanks go to the author...
我发现这个链接是迄今为止最好的入门方式......完整的描述,所以你不会出错。感谢作者...
回答by naren
When we include a video resource in /resource/raw/
or assets/
, by default, it looks for the .mp4
format, it won't accept .wmv
files. If you read video file from external locations(like: /mnt/sdcard/demo.wmv
) then it'll accept them.
当我们在/resource/raw/
or 中包含视频资源时assets/
,默认情况下,它会查找.mp4
格式,它不会接受.wmv
文件。如果您从外部位置(如:)读取视频文件,/mnt/sdcard/demo.wmv
则它会接受它们。
回答by steevoo
Try:
尝试:
AssetFileDescriptor afd = getAssets().openFd(fileName);
player.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(), afd.getLength());
回答by Pelanes
Make sure to write the file video name without extension.
确保写入不带扩展名的文件视频名称。