How do you detect when a sound file has finished?

Not working with broadcast receiver but this is what worked for my current project using Kotlin to track when the audio file has finished playing.

playButton.setOnClickListener {

            if (isPlaying) {
                pauseSound()
            }else {
                playSound()
            }

            mediaPlayer.setOnCompletionListener {
                playButton.text = "Play"
                isPlaying = false
            }
        }

isPlaying is boolean, I will explain more if someone is confused. Hope it helps someone else in the future. Happy coding!


You can use the MediaPlayer class and add a Completion listener to be activated when the sound finishes playing

MediaPlayer mp = MediaPlayer.create(this,Uri.parse("android.resource://emad.app/raw/seven_chimes"));

mp.setOnCompletionListener(new OnCompletionListener() {

    @Override
    public void onCompletion(MediaPlayer mp) {
        performOnEnd();
    }

});

mp.start();