Mohamed
Mohamed

Reputation: 2352

Android Mediaplayer: play multiple sounds

In my app, I have to play many sounds (mp3 files downloaded and stored on SD Card) one after the other. So I used a basic MediaPlayer and I set setOnCompletionListener method to continue playing next media file:

    mediaPlayer = new MediaPlayer();
    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    mediaPlayer.setDataSource(PATH + Constants.Medias.PATH + SLASH + (pref + 1) + SLASH + AppUtils.format(suffix + 1) + AppUtils.format(currentMediaFile));

My big problem is that there is GAP when switching from a file to the next one... It make a strange noise that disturbs users. Have you any idea how to play these files as if they are "one track" (continuously).

Upvotes: 2

Views: 1421

Answers (1)

bonnyz
bonnyz

Reputation: 13548

You can try to create two instances of MediaPlayer (let's call them M1 and M2).

  • M1 starts to play the first file (F1)
  • while M1 is playing, M2 starts preparing for F2 (onlyprepare() is called)
  • when M1 finish, its OnCompletionListener triggers M2 which starts to play
  • continue inverting M1 with M2 until all files have been played.

If this doesn't work and you still hear noise try to use SoundPool instead of MediaPlayer.

Upvotes: 3

Related Questions