How do you detect when a sound file has finished?

前端 未结 3 1264
面向向阳花
面向向阳花 2020-12-05 17:18

I am playing a sound file using this code inside a broadcast receiver activity:

notification.sound = Uri.parse(\"android.resource://emad.app/raw/seven_chimes         


        
相关标签:
3条回答
  • 2020-12-05 17:33

    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!

    0 讨论(0)
  • 2020-12-05 17:38

    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();
    
    0 讨论(0)
  • 2020-12-05 17:44

    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 :)

    0 讨论(0)
提交回复
热议问题