i have a simple app with 2 buttons. When i press Play, the sound run correctly, but when i press Pause and then play, the sound don´t works. Why?
Here my code: import android.app.Activity; import android.media.MediaPlayer; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final MediaPlayer mp = MediaPlayer.create(this, R.raw.sound);
Button Play = (Button) findViewById(R.id.button1);
Play.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
mp.start();
}});
final Button button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mp.stop();
//mp.reset();
}
});
}
}
Don't use stop() for pausing media. Use pause() and check isPlaying() or not before pausing() it.