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.

- 20,200
- 11
- 84
- 98

- 281
- 2
- 6
-
I'm new too, but I do have a question. Maybe your sound has gaps in it? – Not Amused Nov 16 '12 at 02:52
4 Answers
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,560
- 3
- 14
- 28
-
1
-
12
-
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
this is a bug in the android operating system :( http://code.google.com/p/android/issues/detail?id=18756

- 2,650
- 2
- 16
- 21
-
1to 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
-
2I 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
-
1You'll have to use Audacity to actually trim the audio file to make it gapless. – Steven McConnon Jul 19 '16 at 18:07
-
4If 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
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.

- 45,303
- 10
- 103
- 116
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.

- 12,369
- 8
- 43
- 64