How to loop a sound without gaps in Android?
Asked Answered
B

4

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.

Boeotia answered 16/11, 2012 at 2:50 Comment(1)
I'm new too, but I do have a question. Maybe your sound has gaps in it?Machinate
F
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.

Fight answered 16/11, 2012 at 4:36 Comment(4)
Also helped when using SoundPoolHalford
Doesn't take effect in my case :/Deadman
Thanks a lot it almost saved my life, I have invested 3 days finding a solution for same.Eurystheus
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 RingtoneManagerDisrobe
E
7

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

Exuviae answered 16/11, 2012 at 4:24 Comment(4)
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.Exuviae
I tried switching to .OGG, created with Audacity, and did not find it provided gapless playback for my 21 second audio sample.Vaporetto
You'll have to use Audacity to actually trim the audio file to make it gapless.Exuviae
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?Vaporetto
M
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.

Mowery answered 16/11, 2012 at 5:9 Comment(0)
H
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.

Hodosh answered 16/11, 2012 at 2:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.