Android 如何在布局中单击按钮时播放声音?

Android 如何在布局中单击按钮时播放声音?,android,media-player,Android,Media Player,我有一个显示多个按钮的布局。单击按钮分别播放一个声音。我的代码类似于,但不起作用:- @Override public void onClick(View view) { resetPlayer(); int index = 0; if (view.equals(objbutton1)) index = R.raw.sound1; while (true) { this.objplayer = MediaPlayer.create

我有一个显示多个按钮的布局。单击按钮分别播放一个声音。我的代码类似于,但不起作用:-

@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;
    }
应用程序仅播放一种声音,当单击“其他”按钮时,不会更改任何声音

 MediaPlayer mp = MediaPlayer.create(getApplicationContext(), index);
                    mp.start();

 private MediaPlayer mp; // declare it in public area.

    public void onClick(View view) {
    int index = 0;
    if (view.equals(objbutton1)){
        index = R.raw.sound1;
            if(mp !=null && mp.isPlaying()) mp.stop();
        mp = MediaPlayer.create(getApplicationContext(), index);
            mp.start();
        }
        if(view.equals(objbutton2))
        {
            index=R.raw.sound2;
            if(mp !=null && mp.isPlaying()) mp.stop();
        mp = MediaPlayer.create(getApplicationContext(), index);
            mp.start();
        }
        if(view.equals(objbutton3))
        {
            index=R.raw.sound3;
          if(mp !=null && mp.isPlaying()) mp.stop();
          mp = MediaPlayer.create(getApplicationContext(), index);
         mp.start();
        }
        if(view.equals(objbutton4))
        {
            index=R.raw.sound4;
          if(mp !=null && mp.isPlaying()) mp.stop();
          mp = MediaPlayer.create(getApplicationContext(), index);
         mp.start();
        }
        if(view.equals(objbutton5))
        {
            index=R.raw.sound5
          if(mp !=null && mp.isPlaying()) mp.stop();
         mp = MediaPlayer.create(getApplicationContext(), index);
         mp.start();
        }
}

onDestroy()
中放入此
mp.release()

为什么使用
而(真)
?因为我想播放声音直到完成,请建议我这对我不起作用我的要求是,假设我单击第一个按钮声音开始,这里有两种情况,第一种情况是,如果我没有按下任何其他按钮,那么第一个声音将一直播放到完成;另一种情况是,如果我按下其他按钮,那么首先释放,然后播放其他声音。