Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Seamless Looping with SoundPool on Android?

I am trying to loop short (20kb), gapless ogg files with the SoundPool class and cannot get consistent results while testing on hardware. It always plays back perfectly using the emulator but when I test on a Nexus 1, or on a Samsumg Galaxy Tab 10.1 there are audible pops or clicks at every loop point. What is very strange is that while consistent once the application has started, the clicks are slightly different every time I restart the app and on rare occasions (more frequently on the tablet) the loop plays correctly.

The results are no better using MediaPlayer. Is it unreasonable to expect gapless playback of audio loops on android? Surely someone has similar functionality working properly? If so I would love to see an example of how it works.

Thanks!

like image 489
keston Avatar asked Dec 25 '11 21:12

keston


1 Answers

I used a hack that works fine for single files:

HACK_loopTimer = new Timer();
HACK_loopTask = new TimerTask() {               
    @Override public void run() {
        mMediaPlayer.seekTo(0);
    }
};
long waitingTime = mMediaPlayer.getDuration()-mHackLoopingPreview;
HACK_loopTimer.schedule(HACK_loopTask, waitingTime, waitingTime);

Just set mHackLoopingPreview to a reasonable amount; I'm using 100ms and it is working fine. I have to agree that this is a less than ideal and ugly solution, but at least it does its job.

like image 122
Beowulf Bjornson Avatar answered Sep 22 '22 06:09

Beowulf Bjornson