Android Media Player Wont Play After Stop

11,350

Solution 1

to play song again reset media player, set data source again and start

mp.reset();
mp.setDataSource(MEDIA_PATH);
mp.prepare();
mp.start();

Solution 2

When press play button after stop ,then play button never works--for this problem we can create object again in stop button. for eg;-stop.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if(mPlayer.isPlaying())
            mPlayer.stop();
            mPlayer  = MediaPlayer.create(mediaplayeractivity.this, R.raw.adidas);
        }
    });}
Share:
11,350
user2027663
Author by

user2027663

Updated on July 25, 2022

Comments

  • user2027663
    user2027663 almost 2 years

    I have music playlist for 5 songs. I just want that play and stop buttons work as long as im in app. And that i can stop music when i want to and start another.

    How this works now...The music plays on PLAY button, and when i click STOP button it stops, but then i want to play some other song, or same song again, nothing happens. Please help.

    public class glavna extends Activity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);     
        setContentView(R.layout.activity_main);
    
    
    
        final MediaPlayer MPRadio1 = MediaPlayer.create(this, R.raw.pj1);
        final MediaPlayer MPRadio2 = MediaPlayer.create(this, R.raw.pj2);
        final MediaPlayer MPRadio3 = MediaPlayer.create(this, R.raw.pj3);
        final MediaPlayer MPRadio4 = MediaPlayer.create(this, R.raw.pj4);
        final MediaPlayer MPRadio5 = MediaPlayer.create(this, R.raw.pj5);
    
        final RadioButton rb1, rb2, rb3, rb4, rb5;      
    
        rb1 = (RadioButton) findViewById(R.id.radio1);
        rb2 = (RadioButton) findViewById(R.id.radio2);
        rb3 = (RadioButton) findViewById(R.id.radio3);
        rb4 = (RadioButton) findViewById(R.id.radio4);
        rb5 = (RadioButton) findViewById(R.id.radio5);
    
    
        Button btn = (Button) findViewById(R.id.buttonplay);
        Button btnStop = (Button) findViewById(R.id.buttonStop);
    
        btnStop.setOnClickListener(new View.OnClickListener() {
    
        public void onClick(View b){
    
            MPRadio1.stop();
            MPRadio2.stop();
            MPRadio3.stop();
            MPRadio4.stop();
            MPRadio5.stop();
    
    
        };
        });
    
    
        btn.setOnClickListener(new View.OnClickListener() {
    
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
    
    
    
                if(rb1.isChecked())
                {       
    
                MPRadio1.start();
    
                }
            else
                {
                if(rb2.isChecked())
                {
    
                    MPRadio2.start();
                }
                    else
                    {
                        if(rb3.isChecked())
                        {
                        MPRadio3.start();
    
                    }
                        else
                        {
                            if(rb4.isChecked())
                            {
                            MPRadio4.start();
    
                        }
                            else
                            {
                                if(rb5.isChecked())
                                {
                                MPRadio5.start();
    
                                }
    
                            }   
                        }
                    }
                };
            }
        }
    
            );}}
    
  • IcedDante
    IcedDante over 7 years
    I'm sure this is correct, but the state diagram presented in the documentation doesn't seem to support this assertion. I feel like the MediaPlayer API is poorly designed in general.
  • Seabass77
    Seabass77 over 6 years
    This seems like a really weird solution. What are the benefits of having an array of MediaPlayer objects?
  • Allan_Aj5
    Allan_Aj5 over 6 years
    I am not sure how this works .. But anyway we will be using only one MediaPlayer Object ie .. mediaPlayer[0] @Seabass77
  • Seabass77
    Seabass77 over 6 years
    Well I don't think there's ever an instance where you should have more than one media player... but if it works and you don't see any significant performance issues then it guess it's all good Edit: What I mean is that you should pause and start the same media player if you want to play 2 different songs
  • Anand Savjani
    Anand Savjani almost 6 years
    what if song play in looping? how to reset?