Background Music in Android Continously and Forced Close -
well have set of codes activity. have app have more 10 activities. , want have continously background music, stop whenever go activity. , when i'm in activity (also in other activities) when click home button of emu or device, , application "forced closed" hope can me. thankyou
public class advancedkids extends activity implements onclicklistener { mediaplayer yourstereo; assetfiledescriptor afd; public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.advancelay); view numbers = this.findviewbyid(r.id.button1); numbers.setonclicklistener(this); view alphabets = this.findviewbyid(r.id.button2); alphabets.setonclicklistener(this); view colors = this.findviewbyid(r.id.button3); colors.setonclicklistener(this); view shapes = this.findviewbyid(r.id.button4); shapes.setonclicklistener(this); view back= this.findviewbyid(r.id.buttonback); back.setonclicklistener(this); try { // read music file asset folder afd = getassets().openfd("haha.mp3"); // creation of new media player; yourstereo = new mediaplayer(); // set player music source. yourstereo.setdatasource(afd.getfiledescriptor(), afd.getstartoffset(),afd.getlength()); // set looping , play music. yourstereo.setlooping(true); yourstereo.prepare(); yourstereo.start(); } catch (ioexception e) { e.printstacktrace(); } } public void onpause() { super.onpause(); yourstereo.pause(); } public void onresume() { super.onresume(); yourstereo.start(); } protected void onstop() { super.onstop(); yourstereo.stop(); yourstereo = null; } @override public void onbackpressed(){ new alertdialog.builder(this).seticon(android.r.drawable.ic_dialog_alert).settitle("exit") .setmessage("are sure want exit?") .setpositivebutton("yes", new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { intent intent = new intent(intent.action_main); intent.addcategory(intent.category_home); intent.setflags(intent.flag_activity_new_task); startactivity(intent); system.exit(0); } }).setnegativebutton("no", null).show(); } public void onclick(view v) { switch(v.getid()){ case r.id.button1: intent button1 = new intent(this, numbers.class); startactivity(button1); break; case r.id.button2: intent button2 = new intent(this, alphabets.class); startactivity(button2); break; case r.id.button3: intent button3 = new intent(this, colors.class); startactivity(button3); break; case r.id.button4: intent button4 = new intent(this, shapes.class); startactivity(button4); break; case r.id.buttonback: intent buttonback = new intent(this, mainactivity.class); startactivity(buttonback); break; } system.exit(0); } }
create service
public class myservice extends service implements mediaplayer.onpreparedlistener { private static final string action_play = "com.example.action.play"; mediaplayer mmediaplayer = null; public int onstartcommand(intent intent, int flags, int startid) { ... if (intent.getaction().equals(action_play)) { mmediaplayer = ... // initialize here mmediaplayer.setonpreparedlistener(this); mmediaplayer.prepareasync(); // prepare async not block main thread } } /** called when mediaplayer ready */ public void onprepared(mediaplayer player) { player.start(); } }
start it
startservice(new intent(this, myservice.class));
read more here...
Comments
Post a Comment