java - playing background music and the user can stop or restart or pause the music -
i developing android game , play background music our intro (we have intro activity) want continue playing next activity, , perhaps able stop or play music again anywhere within application.
i have created service class
every thing working fine need when user stop music media player save getcurrentposition(); can play music counted seekto(msec);
the main point music not start beginning if user stop music in middle of length (it user can pause music , continue music)
i use
startservice(new intent(this, myservice.class));
to start music
and use
stopservice(new intent(this, myservice.class));
to stop music
this myservice class
public class myservice extends service { private static final string tag = "myservice"; mediaplayer player; @override public ibinder onbind(intent intent) { return null; } @override public void oncreate() { toast.maketext(this, "my service created", toast.length_long).show(); log.d(tag, "oncreate"); player = mediaplayer.create(this, r.raw.game_music3); player.setlooping(false); // set looping } @override public void ondestroy() { toast.maketext(this, "my service stopped", toast.length_long).show(); log.d(tag, "ondestroy"); player.stop(); } @override public void onstart(intent intent, int startid) { toast.maketext(this, "my service started", toast.length_long).show(); log.d(tag, "onstart"); player.start(); } protected void onnewintent() { player.pause(); } }
if thing not clear u please comment on question , try explain more thank ur help
why don't try , save current position of music track on sharedpreferences
?
check storage options android documentation.
Comments
Post a Comment