Android studio help how to detect when a song is finish

I'm trying to automatically play a song once the first song finish, the issue is that I don't know how to tell when the first song is finish. Code is below, I'm trying to play mediaPlayer2 after mediaPlayer is done playing. MediaPlayer mediaPlayer, mediaPlayer2;

public class MainActivity extends AppCompatActivity {

    // global variable
    MediaPlayer mediaPlayer, mediaPlayer2;

    public void play(View view)
        // play music if you want this to start from the beginning put this in oncreate method

        // set loop

    public void pause(View view)
        // pause music

    protected void onCreate(Bundle savedInstanceState) {

        // create media player variable and link it to the music you want
        mediaPlayer = MediaPlayer.create(this, R.raw.a);

        mediaPlayer2 = MediaPlayer.create(this, R.raw.b);


2 answers