i am having difficulty with my app trying to start and stop audio playback. currently i can get it to start, then stop. but i cannot get it to start again. i am using two buttons to help me. here is my code:
public class dnarasol extends Activity {
MediaPlayer mp;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mp = MediaPlayer.create(this, R.raw.freq);
Button stop = (Button)findViewById(R.id.stopButton);
Button play = (Button)findViewById(R.id.playButton);
play.setOnClickListener(
new Button.OnClickListener() {
public void onClick(View v) {
mp.start();
}
});
stop.setOnClickListener(
new Button.OnClickListener() {
public void onClick(View v) {
mp.stop();
mp.release();
}
});
}}
Can anyone see where my problem lies? thankyou very much for any help you can offer.
After you call mp.release()
your player instance is no longer usable.
You should call mp.release()
in onDestroy()
. And in your stop handler just leave stop()
:
stop.setOnClickListener(
new Button.OnClickListener() {
public void onClick(View v) {
mp.stop();
}
});