Sound looping not working well on Android device

SOLVED: MP3 was the culprit. Use wav for short, looped sounds.

I have  a few sounds that are designed to loop seamlessly (sort of). When playing them in the simulator they sound ok, but when running the game in an Android device they do not sound well. There is clearly a gap/pause in the sound where it restarts.

Is there a way around this or is it just how Corona projects sounds on Android devices?

Here’s an example of such a sound/app:

local fuelPumpSound     = audio.loadSound("fuel\_pump.mp3") audio.play(fuelPumpSound, {channel=3,loops=-1}) 

What device are you using?

I’ve noticed that some Android devices struggle with fast loops ~1s or so.

I’m testing on a Google Nexus 4 at the moment.

If this is a problem only for loops shorter than ca 1 second it’s great info! Thanks!

I’ll certainly try to make a bit longer loops and see if it helps.

Well, I haven’t tried to loop longer sound effects. It’s only been the short ones that I’ve had a reason to loop, so the gap/pause may be present when looping longer sounds as well.

One thing I have seen with my testing is that Android 4.2+ devices don’t seem to exhibit this behavior. As an example I have a Nexus 7 running Android 4.4.2 which is perfectly fine when looping short sounds.

I think it might be due to the Low-Latency audio fixes that have been implemented since Android 4.2.

Ah… I understand. So maybe it’s not sound file length related at all… If so, I’m back to square 1 again…

And for the Android version thing - My nexus 4 is running Android 4.4.2 so no excuses there either…

It would have been interesting if the Corona guys could shed some light on this problem.

 And for the Android version thing - My nexus 4 is running Android 4.4.2 so no excuses there either…

Interesting, I haven’t seen this problem on any of the 4.2+ devices I’ve had access to (Google/Samsung devices).

On the other hand what kind of codec do you use.

I’m using Signed 16-bit PCM (WAV) without problem on my Nexus 7.

Shucks, I just saw that you’re using an MP3.

Try converting it to a Signed-16-bit PCM (WAV) file and see if that helps.

I’ll certainly do! 

I downloaded your mp3 file.

It has a 0.047s silence in the beginning and a 0.007s silence at the end, which is part of the issue. 

Even after removing these gaps you have a wave-form problem in that the start and end waveforms are not seamless.

To make matters more complicated, it’s in stereo.  This means that both the left- and right-channel waveforms must match in the start and end of the sound not to have a “click” being heard at the start of each loop.

I’d recommend going mono with this type of sound. It makes editing so much easier.

You can use Audacity or similar application to tweak the audio until it sounds right.

Hi, yes, thanks for the tips about the lead-in/out gaps. The loop was “perfect” when I trimmed it in Audacity, but the gaps seems to have been introduced when I saved the audio file to mp3. I wasn’t aware that this could happen, but I learn every day!

But even with the gaps it still sounded ok-ish in the simulator, but very bad on the device.

I’ve now started using wav instead of mps for there loops and now they sound perfect also on the device.

Thank you very much for your help, ingemar!

Great to hear!

The gaps are a known issue of the MP3 format.

I’m not sure why they’re added, but the gaps are added during export to MP3.

Try OGG format. I’ve been using it and haven’t had any gap issues with looping music.

You’re absolutely right! OGG works flawlessly, looping on my Android device!

I didn’t even know that ogg was supported…

Thanks!

Keep in mind that WAV is cross-platform compatible meaning you only need one set of audio assets for Android/iOS/(Windows phone).

OGG can’t be played on iOS (I also think Windows phone doesn’t support it either).

Oh my… always something…  :o

So, Corona just let’s you use formats that are not cross platform compatible? Without any warning? That is a bit scary and I never even considered this a possibility.

But thanks for the valuable info!

They let you use whatever is supported on the target OS, which is a good thing.

You just need to keep track of which formats are compatible with the OS’s you’re targeting.

Yeah, I guess so. If you just know this and take the proper steps.

…and they do mention this in the API docs for audio.loadSound() etc.

Sorry to see you are having problems with MP3s.  We have not experienced that testing on Samsung Galaxy S4 and Samsung 10 inch tablet.  In fact, we have converted most of our WAV files over to MP3 to reduce file size (non loop files).  We edit with Audacity which does the job just fine.  The sound track does loop and does have a small gap in the loop.  But our sound track is long and has a beginning and an end so its not a problem for us. 

MP3 was the culprit. Use wav for short, looped sounds    This is VERY good to know and will use this info in the future!  Thanks for sharing.

What device are you using?

I’ve noticed that some Android devices struggle with fast loops ~1s or so.