22

I want to loop a sound without gaps in android. I tried to use the code mediaplayer.setLooping(true) to loop the sound, but there is always a little pause when the first sound is over and the next sound start.

Sergey Glotov
  • 20,200
  • 11
  • 84
  • 98
BrantYu
  • 281
  • 2
  • 6

4 Answers4

16

Try using the .ogg file format for the sound files in your application. I had this same issue, and after a lot of research , i tried the .ogg format and it loops without any gaps in android.

Info about .ogg format: http://en.wikipedia.org/wiki/.ogg

You can easily convert your sound file from .mp3 or what have you, to .ogg using the popular open source app VLC Media Player.

  • 1
    Also helped when using `SoundPool` – weston May 13 '13 at 19:16
  • 12
    Doesn't take effect in my case :/ – gkiko Feb 23 '14 at 19:20
  • Thanks a lot it almost saved my life, I have invested 3 days finding a solution for same. – Aman Systematix Jan 04 '17 at 05:58
  • you will need to add a new tag (ANDROID_LOOP = true) using some tag editor to that .ogg. And then if you run it via mediaPlayer it may loop without gaps (depends if your sound actually has gaps) NOTE: doing this you may loose control over mediaplayer.setLooping(true). Helpful for ringtone looping using RingtoneManager – Alex May 07 '20 at 12:25
7

this is a bug in the android operating system :( http://code.google.com/p/android/issues/detail?id=18756

Steven McConnon
  • 2,650
  • 2
  • 16
  • 21
  • 1
    to repeat what others have said, use the OGG format and you're good. I've found that Audacity does a good job of cutting any "blankspace" out of files and saving it to .OGG. Best part is that Audacity is free. – Steven McConnon Mar 11 '13 at 06:28
  • 2
    I tried switching to .OGG, created with Audacity, and did not find it provided gapless playback for my 21 second audio sample. – CODE-REaD Jul 19 '16 at 15:26
  • 1
    You'll have to use Audacity to actually trim the audio file to make it gapless. – Steven McConnon Jul 19 '16 at 18:07
  • 4
    If you mean ensure there is no silence at the start and end of the audio file, yes, I did that. I also adjusted the waveforms to "zero out" at the start and end of the file. I can play the sample with Audacity's "Loop Play" (shift-space) command with no audible gap. Is there something I'm missing? – CODE-REaD Jul 19 '16 at 19:31
3

Also worth noting is that gapless playback was added with Jelly Bean (API level 16). It allows you to set a secondary mediaplayer that should be used for playing a next audio stream, as long as the primary player hasn't finished playing the current audio yet. This could potentially be used to create a gapless 'loop' too.

public void setNextMediaPlayer (MediaPlayer next)

Obviously this won't be of much help for most of the market (currently < 3% of the devices is running JB), but nevertheless worth mentioning.

MH.
  • 45,303
  • 10
  • 103
  • 116
2

It usually works just fine, unless you have a really slow phone.

I would double check the sound file with an audio editor like Audacity, to verify that it doesn't have any silence at the beginning or end of the file.

Robert Estivill
  • 12,369
  • 8
  • 43
  • 64