Android - How to remove MediaPlayer loop delays? -
i set music file loop on mediaplayer game, causes 2 sec delay when loops.
my code:
boolean activatesounds = getintent().getbooleanextra("activate sounds", true); if(mp!=null){ mp.reset(); mp.release(); } mp = mediaplayer.create(startgame.this, r.raw.music1); mp.setvolume(8f, 8f); mp.setlooping(true); // causing delays if (activatesounds) mp.start();
for game, not interesting. there way make mediaplayer run out of loop delays?
i not able make setlooping work without gap.
only solution worked me use setnextmediaplayer call (which able start next loop without gap) , 2 mediaplayers.
'pseudo' code:
class abc implements mediaplayer.oncompletionlistener { private final mediaplayer[] mps = new mediaplayer[2]; public abc() { mps[0] = new mediaplayer(); mps[1] = new mediaplayer(); mps[0].setoncompletionlistener(this); mps[1].setoncompletionlistener(this); } public void start() initmediaplayer(mps[0]); initmediaplayer(mps[1]); mps[0].setnextmediaplayer(mps[1]); mps[0].start(); } private void initmediaplayer(mediaplayer mp) { if (mp.isplaying()){ mp.stop(); } mp.reset(); final float volume = 0.07f; mp.setdatasource(my_source); mp.setvolume(volume, volume); mp.setlooping(false); try { mp.prepare(); }catch(exception error){ log.d("backgroundmusic", error.tostring()); } } @override public void oncompletion(mediaplayer mp) { mediaplayer cur = mps[0] == mp ? mps[1] : mps[0]; initmediaplayer(mp); cur.setnextmediaplayer(mp); } }
Comments
Post a Comment