开发者

Android Raw Array

开发者 https://www.devze.com 2023-02-20 16:18 出处:网络
I have an array of sound int mySounds ={R.raw.sound1,R.raw.sound2,R.raw.sound3} Am later creating a sound by

I have an array of sound

int mySounds ={R.raw.sound1,R.raw.sound2,R.raw.sound3}

Am later creating a sound by

    mp开发者_如何转开发 = MediaPlayer.create(this,mySounds[0] );
    mp.start();

Was wondering if there is anyway to just use strings in the array

String mySounds ={"sound1.mp3","sound2.mp3"}


Yes, it's possible. You have to look up the resource by using getIdentifier

String mySounds ={"sound1","sound2"};
int mp3Resource = getResources().getIdentifier(mySounds[0], "raw" , String packageName);
mp = MediaPlayer.create(this,mp3Resource );
mp.start();

Resource names are without extension. So it's not sound1.mp3, but sound1.

0

精彩评论

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