(1)Activity:
private MediaPlayer mediaPlayer;
private String path;
mediaPlayer = new MediaPlayer();
@Override
public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
switch (position){
case 0:
Toast.makeText(getActivity(), "第一首", Toast.LENGTH_SHORT).show();
mediaPlayer.reset();//重置
path = Environment.getExternalStorageDirectory().getPath() + "/Music/a.mp3";
try {
mediaPlayer.setDataSource(path);//设置数据来源
mediaPlayer.prepare();//准备
mediaPlayer.start();//开始
} catch (IOException e) {
e.printStackTrace();
}
break;
case 1:
Toast.makeText(getActivity(), "第二首", Toast.LENGTH_SHORT).show();
mediaPlayer.reset();
path = Environment.getExternalStorageDirectory().getPath() + "/Music/b.mp3";
try {
mediaPlayer.setDataSource(path);
mediaPlayer.prepare();
mediaPlayer.start();
} catch (IOException e) {
e.printStackTrace();
}
break;
case 2:
Toast.makeText(getActivity(), "第三首", Toast.LENGTH_SHORT).show();
mediaPlayer.reset();
path = Environment.getExternalStorageDirectory().getPath()+"/Music/c.mp3";
try {
mediaPlayer.setDataSource(path);
mediaPlayer.prepare();
mediaPlayer.start();
} catch (IOException e) {
e.printStackTrace();
}
break;
}
}