1

I am try to create a stream player i am using this code to stop the song when another song will play but this code does not work . both songs play together i want to play one by one or if any other song will clicked the stop the previous song and play that song. Any one can help me.

 try {

                MediaPlayer player = new MediaPlayer();

                player.setAudioStreamType(AudioManager.STREAM_MUSIC);
                player.setDataSource(upload.getUrl());
                if(player.isPlaying())
                {
                    player.stop();
                }
               else {

                   player.prepare();
                   player.start();
               }
            } catch (Exception e) {
                // TODO: handle exception
            }
Aabauser
  • 533
  • 1
  • 4
  • 17
A.J
  • 133
  • 1
  • 1
  • 11

2 Answers2

0

You have to release your mediaplayer to stop

player.release();
John Joe
  • 12,412
  • 16
  • 70
  • 135
0

try below code

try {

            MediaPlayer player = new MediaPlayer();

            player.setAudioStreamType(AudioManager.STREAM_MUSIC);
            player.setDataSource(upload.getUrl());
            if(player.isPlaying())
            {
                player.stop();
                player.release();//change here
            }
           else {

               player.prepare();
               player.start();
           }
        } catch (Exception e) {
            // TODO: handle exception
        }
Omkar
  • 3,040
  • 1
  • 22
  • 42