Hi Friends,
i have been developing an application that is store songs in listview and play corresponding songs in our media player. i generated a code, but one problem is that i can to play a song at a time. that is if i select one song to play , that will playing. then i selected other songs both songs are playing concurrently.I took my songs from database to Listview.Can you help me to solve my problem.
My Code is
public class ListDownloadedItem extends Activity{
BookModel bm;
private int currentPosition=0;
BookAdapter bookadpt;
MediaPlayer mp;
ListView listbooks;
//BookAdapter bookadpt;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listdownbook);
listbooks = (ListView) findViewById(R.id.down);
bookadpt = new BookAdapter(ListDownloadedItem.this);
// bookadpt.createDatabase();
bookadpt.open();
final ArrayList<bookmodel> values = bookadpt.selectAll();
ArrayList<bookmodel>bookModels=new ArrayList<bookmodel>();
listbooks.setAdapter(new ArrayAdapter<bookmodel>(this,
android.R.layout.simple_list_item_1, values));
bookadpt.close();
listbooks.setTextFilterEnabled(true);
listbooks.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView int position, long id) {
bm= values.get(position);
playSong(Utilis.DB_PATH + bm.getPartName());
}
});
}
private void playSong(String songPath) {
boolean flag=false;
mp=new MediaPlayer();
//if(flag==true)
mp.stop();
mp.reset();
mp.release();
try {
mp.setDataSource(songPath);
mp.prepare();
mp.start();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
mp.stop();
mp.reset();
mp.release();
}
}