开发者

Problem accessing res/raw resources on phone... works fine in emulator

开发者 https://www.devze.com 2023-02-02 08:35 出处:网络
I am trying to access resources in the /res/raw folder programmatically (specifically... mp3 files). In the emulator, the following template works for me.

I am trying to access resources in the /res/raw folder programmatically (specifically... mp3 files). In the emulator, the following template works for me.

Uri uri = Uri.parse("android.resource://com.foo.bar/raw/name_of_resource");
MediaPlayer mp3 = MediaPlayer.create(this, uri);

On an actual phone, the MediaPlayer.create() method returns null. What gives? Is there a better way of accessing resources programmatically?

P.开发者_JAVA技巧S.

I am not using MediaPlayer.create(this, R.raw.name_of_resource) for a reason. In my particular situation, is much much easier for me to generate the name of the resource than figure out the id of the resource. (i.e. I can figure out the String name_of_resource fairly easily, but deducing the actual integer id R.raw.name_of_resource would involve a a lot more work and a lot more switch statements.


Is there a better way of accessing resources programmatically?

Use the static create() method on MediaPlayer.

In my particular situation, is much much easier for me to generate the name of the resource than figure out the id of the resource.

Use getResources().getIdentifier() to get your resource ID.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号