audio - Double sound android -
package project.kalmas; import android.app.activity; import android.content.intent; import android.media.mediaplayer; import android.os.bundle; import android.view.view; public class 1 extends activity { public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.one); } public void onclick2(view view) { intent i=new intent("project.two"); startactivity(i); } public void onclick3(view view) { mediaplayer mp= mediaplayer.create(this,r.raw.one); if(mp.isplaying()){ mp.stop(); } else { mp.start(); } }
} when click button play sound again click button stop wont stop , play sound again results in double sound playing @ 1 time.please help
you creating new mediaplayer
every click, instead of keeping reference first one. mediaplayer
playing sound different mediaplayer
calling isplaying()
on. need turn mp
variable field can keep reference it.
i imagine work:
public class 1 extends activity { mediaplayer mp; public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.one); mp = mediaplayer.create(this,r.raw.one); } public void onclick2(view view) { intent i=new intent("project.two"); startactivity(i); } public void onclick3(view view) { if(mp.isplaying()){ mp.stop(); } else { mp.start(); } } }
Comments
Post a Comment