我正在使用以下代码从我的res / raw文件播放媒体文件,并且它正常工作。但是,我的res / raw文件夹中有20个mp3文件我应该如何在res / raw文件夹中创建mp3文件的ArrayList?
MediaPlayer mediaPlayer = MediaPlayer.create(context, R.raw.music_file);
mediaPlayer.start();
下面是我的完整代码;我无法播放第二个音频文件,
public class Datadetail extends AppCompatActivity {
private Context context;
TextView txtname;
TextView txtaarti;
String[] itemname;
int position;
ImageButton buttonPlay;
int[] songlist = {R.raw.ganesh_aarti, R.raw.durga_aarti};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.data_detail);
Intent i = getIntent();
position = i.getExtras().getInt("position");
itemname = i.getStringArrayExtra("itemname");
txtname = (TextView) findViewById(R.id.textView2);
txtname.setText(itemname[position]);
txtaarti = (TextView) findViewById(R.id.textView3);
txtaarti.setText(Aarti.aartitxt[position]);
final MediaPlayer[] mPlayer = {MediaPlayer.create(this, songlist[0])};
buttonPlay = (ImageButton) findViewById(R.id.imageButton);
buttonPlay.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (position==0) {
mPlayer[0] = MediaPlayer.create(getApplicationContext(), R.raw.ganesh_aarti);
mPlayer[0].start();
}
if (position==1){
mPlayer[1] = MediaPlayer.create(getApplicationContext(), R.raw.durga_aarti);
mPlayer[1].start();
}
}
});
}
答案 0 :(得分:0)
public void listRaw(){
Field[] fields=R.raw.class.getFields();
for(int count=0; count < fields.length; count++){
Log.i("Raw Asset: ", fields[count].getName());
int resourceID=fields[count].getInt(fields[count]);
mArrayList.add(resourceID);
}
}
在此之后,您将每个音乐文件放在一个arraylist中,相应地播放它们:)