Saturday, February 23, 2019

Resume music

When play music, if you create and play music like this.
MediaPlayer mPlayer;
mPlayer = MediaPlayer.create(playmusic.this, R.raw.sound);
mPlayer.start();
Music will play normaly.
If when open class, you not use the line mPlayer.start(); but put it in a button. Sometime you click button but no music play.
Or you open new class and go back to press Play, nothing happen, seem it ‘s lose something.
To solve this, add these lines.
@Override
public void onResume() {
     super.onResume();
mPlayer = MediaPlayer.create(playmusic.this, R.raw.sound);
}

Now it play well whenever you press button mPlayer.start();

No comments:

Post a Comment