如何使用一个变量的原始文件夹来访问视频? [英] How to access a video from the raw folder using a variable?

查看:198
本文介绍了如何使用一个变量的原始文件夹来访问视频?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的code:

VideoView vd;
vd = (VideoView) findViewById(R.id.videoview2);
String path = "android.resource://" + getPackageName() + "/"
                        + R.raw.video1;
                vd.setVideoURI(Uri.parse(path));
                vd.start();

这工作,但我想R.raw.video1是一个字串becaus我有很多的视频播放。

This works, but i want the R.raw.video1 to be a string becaus i have a lot of videos to play.

所以我想是这样的:

String videoResource = "R.raw.video1"
 String path = "android.resource://" + getPackageName() + "/"
                            + videoResource;

不幸的是这不工作,我如何得到它的工作?

Unfortunately this doesn't work, how do i get it to work?

推荐答案

您需要的资源,要工作,可以通过它的名字用检索的标识符:

You need the identifier of the resource for that to work, which can be retrieved by its name using:

int id = getResources().
    getIdentifier("name_of_resource", "id", getPackageName());

所以,新的code将变成:

So your new code would become:

int videoResource = getResources().
    getIdentifier("video1", "raw", getPackageName());
String path = "android.resource://" + getPackageName() + "/" + videoResource;

这篇关于如何使用一个变量的原始文件夹来访问视频?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆