我有一个停止按钮和一个播放按钮。当我单击播放按钮时,音频播放正常,当我按下停止时,音频停止。但是,如果我意外地连续两次点击停止按钮,则会抛出IllegalStateException并强制关闭。我怎样才能保持这种力量不再发生。基本上我想要的是,如果有人意外地两次点击停止按钮,程序什么都不做。这是代码:
private MediaPlayer mp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.alphabet);
Button play = (Button) findViewById(R.id.button1);
play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mp = MediaPlayer.create(getBaseContext(), R.raw.e_1100);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
});
Button stop = (Button) findViewById(R.id.button2);
stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (mp != null && mp.isPlaying()) {
mp.stop();
mp.release();
}
else if (mp.isPlaying() == false) {
// What do I put here???
}
}
});
}
}
答案 0 :(得分:0)
试试这个
if (mediaPlayer != null) {
if (mediaPlayer.isPlaying()) {
mediaPlayer.stop();
}
mediaPlayer.reset();
mediaPlayer.release();
mediaPlayer = null;
}