任何人都知道如何检查是否正在播放MediaPlayer。在测试中,在我添加if语句之前,音频播放正常,但现在我关闭了一个力。我想要的是当有人点击另一个按钮时,当前音频停止并播放新音频。我认为这很容易,但似乎没有。我以为我正确地做了代码。我的代码是否正确?我错过了什么?任何帮助将不胜感激。
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View view, int pos,
long id) {
if (mp.isPlaying()) {
mp.stop();
mp.release();
}
else {
if (pos==0) {
mp = MediaPlayer.create(getBaseContext(), R.raw.v_2200);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
if (pos==1) {
mp = MediaPlayer.create(getBaseContext(), R.raw.v_2201);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
if (pos==2) {
mp = MediaPlayer.create(getBaseContext(), R.raw.v_2202);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
if (pos==3) {
mp = MediaPlayer.create(getBaseContext(), R.raw.v_2203);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
if (pos==4) {
mp = MediaPlayer.create(getBaseContext(), R.raw.v_2204);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
if (pos==5) {
mp = MediaPlayer.create(getBaseContext(), R.raw.v_2205);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
if (pos==6) {
mp = MediaPlayer.create(getBaseContext(), R.raw.v_2206);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
if (pos==7) {
mp = MediaPlayer.create(getBaseContext(), R.raw.v_2207);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
if (pos==8) {
mp = MediaPlayer.create(getBaseContext(), R.raw.v_2208);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
if (pos==9) {
mp = MediaPlayer.create(getBaseContext(), R.raw.v_2209);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
if (pos==10) {
mp = MediaPlayer.create(getBaseContext(), R.raw.v_2210);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
if (pos==11) {
mp = MediaPlayer.create(getBaseContext(), R.raw.v_2211);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
if (pos==12) {
mp = MediaPlayer.create(getBaseContext(), R.raw.v_2212);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
if (pos==13) {
mp = MediaPlayer.create(getBaseContext(), R.raw.v_2213);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
if (pos==14) {
mp = MediaPlayer.create(getBaseContext(), R.raw.v_2214);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
if (pos==15) {
mp = MediaPlayer.create(getBaseContext(), R.raw.v_2215);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
if (pos==16) {
mp = MediaPlayer.create(getBaseContext(), R.raw.v_2216);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
}
}
});
答案 0 :(得分:0)
在调用isPlaying()之前检查null,并在所有释放调用之后将其设置为null,因此释放不会被调用两次。
if (mp != null && mp.isPlaying()) {
mp.stop();
mp.release();
mp = null;
}