我有一组声音
int mySounds ={R.raw.sound1,R.raw.sound2,R.raw.sound3}
我稍后通过
创建声音 mp = MediaPlayer.create(this,mySounds[0] );
mp.start();
想知道是否可以只在数组中使用字符串
String mySounds ={"sound1.mp3","sound2.mp3"}
最佳答案
是的,这是可能的。您必须使用 getIdentifier 查找资源
String mySounds ={"sound1","sound2"};
int mp3Resource = getResources().getIdentifier(mySounds[0], "raw" , String packageName);
mp = MediaPlayer.create(this,mp3Resource );
mp.start();
资源名称没有扩展名。所以它不是 sound1.mp3,而是 sound1。
关于安卓原始数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5452312/