59

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

I would like to detect when this file has finished playing.

I tried a loop like this but I think this is only good for the media player because it always = false even though I made sure the sound file was still playing:

/*
 * Stay here until the chime sound is finished.
 */
   while (manager.isMusicActive()){
   }

Can you show me what I should use instead of manager.isMusicActive()?

Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
Emad-ud-deen
  • 4,734
  • 21
  • 87
  • 152

3 Answers3

140

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();
articga
  • 375
  • 3
  • 15
Ronny
  • 2,399
  • 3
  • 20
  • 27
  • Hi Ronny, I tried the code but get an error that states: MediaPlayer is not applicable for the arguments (new OnCompletionListener(){}). – Emad-ud-deen Sep 10 '11 at 09:13
  • 4
    Hi Emad, you need to add import android.media.MediaPlayer.OnCompletionListener; so it will recognize it. – Ronny Sep 10 '11 at 15:50
  • Hi Ronny, I added the import and had to change "this" to "context" but I still get the same error on the OnCletionListener line. Is something still missing? Thanks. Truly, Emad – Emad-ud-deen Sep 10 '11 at 20:15
  • Hi, this is a strange because it's a very strait-forward approche. What is the API version you are using? what is the error message? and what are the Eclipse suggestions when you press control+1 (or command + 1)? you can pass "this" as the argument, mp.setOnCompletionListener(this); and it will suggest you to implement the OnCompletionListener and you can add the onCompletion method to your class. Hope it helps :) – Ronny Sep 10 '11 at 22:11
  • Hi Ronny, It's for Android 1.6 and is within a broadcast receiver activity. I found I had to also include "import android.media.MediaPlayer.OnCompletionListener;" to get it without errors. Thanks so much for the help. Truly, Emad – Emad-ud-deen Sep 11 '11 at 02:57
3

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!

Mr. Disability
  • 799
  • 1
  • 10
  • 19
1

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

Michael Frans
  • 613
  • 3
  • 23
  • 49
  • Hi Michael, I would like to also try your way as well. Can you show a code example for me to try out? Thanks. Truly, Emad – Emad-ud-deen Sep 10 '11 at 08:55