java - Android - Need to Pause playing mediafile at particular instance -
i have page consisting of 3 different buttons play | pause | stop. play | stop working fine, i'm not able pause @ particular instance. want pause playing @ instance, pressing pause button. , resume saved instance again pressing play button.
below code sound.java file
public class sound extends activity { mediaplayer mp = null; string play = "play!"; string stop = "stop!"; string pause = "pause"; int length; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.sound); final imagebutton play = (imagebutton) findviewbyid(r.id.idplay); play.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { managerofsound("play"); toast toast_play = toast.maketext(getbasecontext(), "playing first", toast.length_short); toast_play.show(); } // end onclick() }); final imagebutton pause = (imagebutton) findviewbyid(r.id.idpause); pause.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { managerofsound("pause"); toast toast_pause = toast.maketext(getbasecontext(), "pausing morning bhajan-aarati", toast.length_short); toast_pause.show(); } // end onclick() }); final imagebutton stop = (imagebutton) findviewbyid(r.id.idstop); stop.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { if (mp != null) { mp.stop(); mp.reset(); toast toast_stop = toast.maketext(getbasecontext(), "stopping first", toast.length_short); toast_stop.show(); } } // end onclick() }); /** manager of sounds **/ protected void managerofsound(string thetext) { if (mp != null){ mp.reset(); mp.release(); } if (thetext == "play") mp = mediaplayer.create(this, r.raw.goodbye); else if (thetext == "pause" && mp.isplaying()) mp.pause(); length = mp.getcurrentposition(); mp.seekto(length); mp.start(); }
here sound.xml file,
<relativelayout android:layout_width="wrap_content" android:layout_height="wrap_content" > <imagebutton android:id="@+id/idplay" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginright="65dp" android:layout_margintop="20dp" android:background="@drawable/image_play" /> <!-- android:text="@string/idplay" /> --> <imagebutton android:id="@+id/idpause" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margintop="20dp" android:layout_toleftof="@+id/idstop" android:background="@drawable/image_pause" /> <imagebutton android:id="@+id/idstop" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginleft="20dp" android:layout_margintop="20dp" android:layout_torightof="@+id/idplay" android:background="@drawable/image_stop" /> </relativelayout>
your appreciated..!!
i think problem line:
if (thetext == "play") mp = mediaplayer.create(this, r.raw.goodbye); else if (thetext == "pause" && mp.isplaying()) mp.pause(); length = mp.getcurrentposition(); mp.seekto(length); mp.start();´
you setting length clicking pause button, start again directly in if clause.
you should check if play button clicked if media playing befor, like
if(length > 0)
and start player @ specified position.
edit: in addition, should not use operator ==
compare strings. should better use string1.equals(string2)
hope helps
best wishes
Comments
Post a Comment