0

i try to stream and play an audio file in my application. i use this code :

MediaPlayer player = new MediaPlayer();
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
player.setDataSource(url);
player.prepare();
player.start();

i want to know is it possible to detect when player finished playing ? i want to change a button color after player finished playing

Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
mahdi
  • 16,257
  • 15
  • 52
  • 73

1 Answers1

6

Check out the setOnCompletionListener method:

player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
    @Override
    public void onCompletion(MediaPlayer mp) {
        Log.d(TAG, "Done playing!");
    }
});

You can actually set a ton of callbacks here. In addition to setOnCompletionListener, we have:

  • setOnBufferingUpdateListener
  • setOnErrorListener
  • setOnInfoListener
  • setOnPreparedListener
  • setOnSeekCompleteListener
  • setOnTimedMetaDataAvailableListener
  • setOnTimedTextListener
  • setOnVideoSizeChangedListener.
Anubian Noob
  • 13,426
  • 6
  • 53
  • 75