我有一個佈局,其中多個按鈕的按鈕一個聲音播放respectively.My代碼showing.onclick就像但它不起作用: -如何在佈局中播放按鈕的聲音onclick?
@Override
public void onClick(View view) {
resetPlayer();
int index = 0;
if (view.equals(objbutton1))
index = R.raw.sound1;
while (true) {
this.objplayer = MediaPlayer.create(this, index);
this.objplayer.setLooping(false);
this.objplayer.start();
this.objplayer
.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
MainActivity.this.objplayer.release();
MainActivity.this.objplayer = null;
}
}
);
if(view.equals(objbutton2))
{
index=R.raw.sound2;
continue;
}
if(view.equals(objbutton3))
{
index=R.raw.sound3;
continue;
}
if(view.equals(objbutton4))
{
index=R.raw.sound4;
continue;
}
if(view.equals(objbutton5))
{
index=R.raw.sound5;
continue;
}
break;
}
}
private void resetPlayer() {
if (this.objplayer != null) {
if (this.objplayer.isPlaying())
this.objplayer.stop();
this.objplayer.release();
this.objplayer = null;
}
應用程序是隻打了一個聲音,當點擊另一個按鈕沒有健全的改變
爲什麼你使用'while(true)'? – FoamyGuy 2013-04-06 14:52:32
,因爲我想播放聲音直到完成,請給我建議 – SKT 2013-04-06 14:53:46