Skip to content Skip to sidebar Skip to footer

Multiple Songs Are Playing When I Click On Listview In Mediaplayer

Hello I am creating Music player app. I have a problem in that when I click on listview it plays song. When once again I clicked on listview then songs are playing together. Previo

Solution 1:

Because you are creating new Instance of MediaPlayer every-time in your activity.

Use a single Instance of MediaPlayer.

Create a singleton class and and use MediaPlayer from there.

Or you can make MediaPlayer as public static in your ListView Activity.

publicstaticMediaPlayermp=newMediaPlayer();

And use this MediaPlayer in your now_playing Activity

Solution 2:

I had the same issue and fixed like this. Change to your project requirement.

Declare your mediaPlayer Instance globally

staticMediaPlayermediaPlayer=newMediaPlayer();

Create your AudioManager and Volume setup (note: I used Max Volume to play the sound):

AudioManagermAudioManager= (AudioManager) getSystemService(AUDIO_SERVICE);
        intmaxVolume= mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, maxVolume, AudioManager.FLAG_PLAY_SOUND);

Then put this code on your Play button onClick

try {

            if (mediaPlayer != null) {
                if (mediaPlayer.isPlaying()) {
                    mediaPlayer.stop();
                }
                mediaPlayer.release();
                mediaPlayer = null;
            }

            mediaPlayer.setDataSource(filePath);
            mediaPlayer.prepare();
            mediaPlayer.start();

        } catch (IOException e) {
            e.printStackTrace();
        }

Post a Comment for "Multiple Songs Are Playing When I Click On Listview In Mediaplayer"