我尝试使用可扩展列表视图创建回溯应用程序。起初它正确播放但我发现了一个错误,当我尝试点击它仍然播放时的标题。 我不知道如何避免这首歌播放过两次。 我不知道做对了,已经尝试使用isPlaying但失败了。 有人帮助我。
public class BacktrackActivity extends Activity {
MediaPlayer mp;
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
Context context;
public ImageButton stop;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.backtrack);
// final int[] blues = {R.raw.satch_boogie, R.raw.satch_boogie};
// mp = MediaPlayer.create(this, R.raw.satch_boogie);
// get the listview
expListView = (ExpandableListView) findViewById(R.id.genre_list);
// preparing list data
prepareListData();
listAdapter = new ExpandableListAdapter(this,listDataHeader,listDataChild);
// setting list adapter
expListView.setAdapter(listAdapter);
stop = (ImageButton) findViewById(R.id.imageButton1);
stop.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
mp.stop();
mp.reset();
}
});
expListView.setOnGroupClickListener(new OnGroupClickListener() {
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition,
long id) {
// TODO Auto-generated method stub
return false;
}
});
expListView.setOnGroupExpandListener(new OnGroupExpandListener() {
@Override
public void onGroupExpand(int groupPosition) {
Toast.makeText(getApplicationContext(), "Choose your " +
listDataHeader.get(groupPosition) + " Backtrack",
Toast.LENGTH_SHORT).show();
}
});
// Listview on child click listener
expListView.setOnChildClickListener(new OnChildClickListener() {
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
if(groupPosition == 0 && childPosition == 0){
mp.stop();
mp = MediaPlayer.create(BacktrackActivity.this, R.raw.satch_boogie);
mp.start();
}
if(groupPosition == 0 && childPosition == 1){
mp = MediaPlayer.create(BacktrackActivity.this, R.raw.top_gun);
mp.start();
}
return false;
}
});
}
/*
* Preparing the list data
*/
private void prepareListData() {
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<String>>();
// Adding child data
listDataHeader.add("Blues");
listDataHeader.add("Jazz");
listDataHeader.add("Rock");
listDataHeader.add("RnB");
// Adding child data
List<String> Blues = new ArrayList<String>();
Blues.add("The Shawshank Redemption");
Blues.add("The Godfather");
Blues.add("The Godfather: Part II");
Blues.add("Pulp Fiction");
Blues.add("The Good, the Bad and the Ugly");
Blues.add("The Dark Knight");
Blues.add("12 Angry Men");
List<String> Jazz = new ArrayList<String>();
Jazz.add("The Conjuring");
Jazz.add("Despicable Me 2");
Jazz.add("Turbo");
Jazz.add("Grown Ups 2");
Jazz.add("Red 2");
Jazz.add("The Wolverine");
List<String> Rock = new ArrayList<String>();
Rock.add("2 Guns");
Rock.add("The Smurfs 2");
Rock.add("The Spectacular Now");
Rock.add("The Canyons");
Rock.add("Europa Report");
List<String> RnB = new ArrayList<String>();
RnB.add("2 Guns");
RnB.add("The Smurfs 2");
RnB.add("The Spectacular Now");
RnB.add("The Canyons");
RnB.add("Europa Report");
listDataChild.put(listDataHeader.get(0), Blues); // Header, Child data
listDataChild.put(listDataHeader.get(1), Jazz);
listDataChild.put(listDataHeader.get(2), Rock);
listDataChild.put(listDataHeader.get(3), RnB);
}
}
答案 0 :(得分:0)
不要使用静态create
方法,而是在声明处调用MediaPlayer mp = new MediaPlayer();
。然后代替create
调用,执行类似的操作(您的代码会被注释以显示它的位置):
//mp.stop();
//mp = MediaPlayer.create(BacktrackActivity.this, R.raw.satch_boogie);
mp.reset();
mp.setDataSource(BacktrackActivity.this, Uri.parse(
// I don't know your package name...
"android.resource://com.your.package/" + R.raw.satch_boogie));
mp.prepare();
mp.start();
仅使用MediaPlayer
的一个实例应该保持多个来源一次播放。此外,请在完成后致电release()
(可能在onDestroy
)。