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
        mediaPlayer.start();

        // set loop
        mediaPlayer.setLooping(true);
    }



    public void pause(View view)
    {
        // pause music
        mediaPlayer.pause();
    }


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 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