pencilvania pencilvania - 11 months ago 104
Java Question

Play and stop MediaPlayer in Android

Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
MediaPlayer mp = MediaPlayer.create(main.this, R.raw.lastmohican);
public void onClick(View arg0) {
// TODO Auto-generated method stub
if (mp.isPlaying()) {
mp.stop();
} else {
mp.start();
}
}
}


I can play this music by one click and with another click music is stopped, but when I click together music does not start. Any idea why?

Answer Source

Its solved you should change code for this type

if (mp.isPlaying()) {
    mp.stop();
    mp.prepareAsync();
    mp.seekTo(0);
} else {
    mp.start();
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download