Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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();

}

}
Posted
Comments
Member 10893967 22-Jul-14 0:46am    
sorry, i have one mistake typed.Corrected thing is I cant to play a song at a time.

1 solution

when you close your activity .. just add a simple line code.
finish();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900