Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
232 views
in Technique[技术] by (71.8m points)

android - How do you detect when a sound file has finished?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...