k.h.b
k.h.b

Reputation: 13

How can i stop playing a song when another song start playing in Android

Hello everyone i a listview with items in it. The moment you click on a item you hear a song but if you click on another item the first song doesn't stop it plays two songs at the same time how can i stop the first song if i click the second song or stop the second one if i click the first song

my code

private MediaPlayer mp;

  final ListView listView = (ListView) findViewById(R.id.listView);
        listView.setAdapter(new CustomListAdapter(this, image_details));


        // When the user clicks on the ListItem
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> a, View v, int position, long id) {

                Object o = listView.getItemAtPosition(position);
                final Songs titel = (titel) o;



                // get the club song from the arraylist
                mp = MediaPlayer.create(a.getContext(), titel.getClubLied());
                mp.start();

               mp.stop();
        });






 listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                mp.stop();
                return  true;
            }
        });

How can i fix this i used start and stop so the moment other song starts it stops but this doesnt work

Upvotes: 1

Views: 2650

Answers (4)

Anupam
Anupam

Reputation: 41

You can declare the MediaPlayer as static so that only 1 item runs at a time.

Upvotes: 1

Upendra Shah
Upendra Shah

Reputation: 2301

You are creating mediaplayer in on setOnItemClickListener so whenever you click item it creates new mediaplayer so previous one is play continue so create media player outside the setOnItemClickListener than pass only song data..

private MediaPlayer mp;

  final ListView listView = (ListView) findViewById(R.id.listView);
        listView.setAdapter(new CustomListAdapter(this, image_details));

mp = new MediaPlayer();
        // When the user clicks on the ListItem
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> a, View v, int position, long id) {

                Object o = listView.getItemAtPosition(position);
                final Songs titel = (titel) o;



                // get the club song from the arraylist
        mp.reset()
        mp.setDataSource(a.getContext(), uri);
        mp.prepare();
        mp.start();


        });






 listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                mp.stop();
                return  true;
            }
        });

Upvotes: 0

Shivam Sharma
Shivam Sharma

Reputation: 418

you can check if MediaPlayer is playing then stop or release it first then play new song like that

           if (mp.isPlaying()) {
                 mp.stop();
                 mp.reset();
                 mp.release();
            }
            mp = MediaPlayer.create(a.getContext(), titel.getClubLied());
            mp.start();

Hope its work;

Upvotes: 1

aborocz
aborocz

Reputation: 199

You have to release your mediaplayer to stop the mediaplayer like below:

@Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {

            Object o = listView.getItemAtPosition(position);
            final Songs titel = (titel) o;

            if (mp != null) {
                mp.stop();
                mp.reset();
                mp.release();
            }

            mp = MediaPlayer.create(a.getContext(), titel.getClubLied());
            mp.start();
    });

Upvotes: 0

Related Questions