Carlos Pereira
Carlos Pereira

Reputation: 1924

Playing media in Android

my app is playing some audio files, but if the user hits the play button twice, it will play then it will play again (something like that).

I would like the next action to happen JUST when the first execution of the audio file is over.

How can I do that?

Here is my code:

MediaPlayer mMediaPlayer = new MediaPlayer();
try {
    mMediaPlayer.setDataSource((endMidia));
    mMediaPlayer.prepare();
} catch (IllegalArgumentException e) {
    e.printStackTrace();
}                                        
mMediaPlayer.start();

Any help is appreciatted!

Upvotes: 0

Views: 125

Answers (3)

Khan
Khan

Reputation: 7605

check if condition as shown in below code

  MediaPlayer mMediaPlayer = new MediaPlayer();
  try {
    mMediaPlayer.setDataSource((endMidia));
    mMediaPlayer.prepare();
      } catch (IllegalArgumentException e) {
      e.printStackTrace();
    }    
   if(!mMediaPlayer.isPlaying()){                                    
        mMediaPlayer.start();
    }

Upvotes: 1

Alexander
Alexander

Reputation: 48262

There are listeners on MediaPlayer events like OnCompletionListener

http://developer.android.com/reference/android/media/MediaPlayer.html#setOnCompletionListener(android.media.MediaPlayer.OnCompletionListener)

and others

Upvotes: 1

biegleux
biegleux

Reputation: 13247

You can make check with MediaPlayer.isPlaying().

Upvotes: 1

Related Questions