android - Resume MediaPlayer after pressing Home Button -
i have mediaplayer streams music in background, when press home button music stops, want, when resume application, music not there anymore. , when current track ends, doesn't loop.
my code following:
mediaplayer backgroundmusic; int length = 0; //play background music backgroundmusic = mediaplayer.create(mainactivity.this, r.raw.background_music); backgroundmusic.start(); backgroundmusic.setlooping(true); @override protected void onpause() { super.onpause(); if(backgroundmusic.isplaying()){ backgroundmusic.pause(); length = backgroundmusic.getcurrentposition(); }else{ return; } } public void onprepared(mediaplayer backgroundmusic){ backgroundmusic.start(); backgroundmusic.seekto(length); }
how can resume music stopped, when press home button, , make music loop.
thanks in advance.
try implement code service class if want player play in background or if want store position save data in static variable in onpause , restore onresume of activity.
Comments
Post a Comment