Leonard
Leonard

Reputation: 79

Stop music mediaPlayer

I have a simple music app. When I click a song on the list the music play, but when I click another music, the music before didn't stop. Could someone help me to solve this? Thanks.

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            final MediaPlayer mp = new MediaPlayer();
            Uri uri = arrayList.get(position).getUri();
            try {
                mp.setDataSource(view.getContext(),uri);
                mp.prepare();
                mp.start();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    });

Upvotes: 0

Views: 41

Answers (1)

Jujare Vinayak
Jujare Vinayak

Reputation: 123

Make MediaPlayer object as global. Don't create it everytime on item click.

Upvotes: 1

Related Questions