I'm facing trouble with my application, what i want to do is when i click the child of exp list it will play song. it's clear when start play first song but after stop and I play it again an error display in my logcat. I don't know how to solve this problem. please help...
Here's my code:
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) {
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);
}
}
here's my logcat error
12-04 08:52:54.317: W/Trace(1413): Unexpected value from nativeGetEnabledTags: 0 12-04 08:52:54.336: E/MediaPlayer(1413): start called in state 1 12-04 08:52:54.336: E/MediaPlayer(1413): error (-38, 0) 12-04 08:52:54.356: W/Trace(1413): Unexpected value from nativeGetEnabledTags: 0 12-04 08:52:54.356: W/Trace(1413): Unexpected value from nativeGetEnabledTags: 0 12-04 08:52:54.416: W/Trace(1413): Unexpected value from nativeGetEnabledTags: 0 12-04 08:52:54.416: W/Trace(1413): Unexpected value from nativeGetEnabledTags: 0 12-04 08:52:54.466: E/MediaPlayer(1413): Error (-38,0)
Usually this kind of error happens when you try to call start() before prepare() is finished.
Before calling mp.start(); you should make sure that you have already set the setDataSource() and prepare() is completed.
In your code by clicking stop, you have reset the media.So before calling mp.start() again you should at least setDataSource() again.
IMHO, it worked the first time because you have set the data source ( MediaPlayer.create(this, R.raw.satch_boogie); )inside the onCreate() method and there is enough time for it to prepared by the time you click the listView child.
Try setDataSource (MediaPlayer.create(this, R.raw.satch_boogie);) anytime you reset it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With