我使用以下代码从我的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();
}
}
});
}