Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you detect when a sound file has finished?

People also ask

Which mediaplayer callback method will notify us when the mediaplayer has finished playing the audio file?

OnCompletionListener. Interface definition for a callback to be invoked when playback of a media source has completed.

How do I know if JavaScript is playing audio?

To check if audio is playing with JavaScript, we can use the paused property of the audio element. const isPlaying = (audElem) => { return !


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();

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!


try using service to play the music.. after it play once, then stop your service and you can add your program on the onDestroy() service method.. hope it helps :)