Search code examples
androidandroid-mediaplayer

Android Media player play the song x times


I would like to play the song x times like 1,2,3 ..etc using android media player.Please let me know is there any way to do this using android api. Thanks


Solution

  • As Wamasa said, you could use setLooping for infinite playing. For playing only a specific count time, you can add an onCompletionListener to your MediaPlayer:

    int count = 0; // initialise outside listener to prevent looping
    
    mediaPlayer.setOnCompletionListener(new OnCompletionListener(){
      int maxCount = 3;
    
      @Override
      public void onCompletion(MediaPlayer mediaPlayer) {
        if(count < maxCount) {
          count++;
          mediaPlayer.seekTo(0);
          mediaPlayer.start();
        }
    }});