当我启动另一个音频时如何停止媒体播放器?

how to stop media player when I start another audio?

我正在尝试开发一个应用程序。此应用程序中有 12 个按钮。每个按钮播放一种不同的音频。每三个按钮都是相同的颜色。我不想一起玩相同的颜色。但是我想一起玩不一样的颜色

现在大家一起玩了。我怎样才能停止玩相同的颜色。

public class Sayfa2 extends AppCompatActivity {
private MediaPlayer sesb1;
private ImageButton b1;
private ImageButton b2;
private ImageButton b3;
private ImageButton b4;
private ImageButton b5;
private ImageButton b6;
private ImageButton b7;
private ImageButton b8;
private ImageButton b9;
private ImageButton b10;
private ImageButton b11;
private ImageButton b12;

private AdView madview;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);//UYUMA ENGELLEME KODU
    setContentView(R.layout.activity_sayfa2);

    MobileAds.initialize(this, new OnInitializationCompleteListener() {
        @Override
        public void onInitializationComplete(InitializationStatus initializationStatus) {
        }
    });

    MobileAds.initialize(this, "ca-app-pub-3206398076180977~5758879718");
    AdView adView = new AdView(this);
    adView.setAdSize(AdSize.BANNER);
    adView.setAdUnitId("ca-app-pub-3206398076180977/5571177453");

    madview = findViewById(R.id.adView);
    AdRequest adRequest = new AdRequest.Builder().build();
    madview.loadAd(adRequest);

    sesb1 = MediaPlayer.create(Sayfa2.this, R.raw.a1);



    b1 = (ImageButton) findViewById(R.id.b1);
    b1.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {

            if (event.getAction() == MotionEvent.ACTION_DOWN) {

               if (sesb1 != null) {
                    sesb1.stop();
                    sesb1.release();
                }


                sesb1 = MediaPlayer.create(Sayfa2.this, R.raw.a11);
                sesb1.start();
                //sesbilgisi();

            }
            return true;
        }
    });

    b2 = (ImageButton) findViewById(R.id.b2);
    b2.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {

            if (event.getAction() == MotionEvent.ACTION_DOWN) {

                /*if (sesb1 != null) {
                    sesb1.stop();
                    sesb1.release();
                }*/
                sesb1 = MediaPlayer.create(Sayfa2.this, R.raw.a21);
                sesb1.start();
                //sesbilgisi();
            }
            return true;
        }
    });

    b3 = (ImageButton) findViewById(R.id.b3);
    b3.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {

            if (event.getAction() == MotionEvent.ACTION_DOWN) {

               /* if (sesb1 != null) {
                    sesb1.stop();
                    sesb1.release();
                }*/
                sesb1 = MediaPlayer.create(Sayfa2.this, R.raw.a31);
                sesb1.start();
                //sesbilgisi();
            }
            return true;
        }
    });

    b4 = (ImageButton) findViewById(R.id.b4);
    b4.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {

            if (event.getAction() == MotionEvent.ACTION_DOWN) {

               /* if (sesb1 != null) {
                    sesb1.stop();
                    sesb1.release();
                }*/
                sesb1 = MediaPlayer.create(Sayfa2.this, R.raw.a41);
                sesb1.start();
                //sesbilgisi();
            }
            return true;
        }
    });

    b5 = (ImageButton) findViewById(R.id.b5);
    b5.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {

            if (event.getAction() == MotionEvent.ACTION_DOWN) {

               /* if (sesb1 != null) {
                    sesb1.stop();
                    sesb1.release();
                }*/
                sesb1 = MediaPlayer.create(Sayfa2.this, R.raw.b11);
                sesb1.start();
                //sesbilgisi();
            }
            return true;
        }
    });

    b6 = (ImageButton) findViewById(R.id.b6);
    b6.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {

            if (event.getAction() == MotionEvent.ACTION_DOWN) {

                /* if (sesb1 != null) {
                    sesb1.stop();
                    sesb1.release();
                }*/
                sesb1 = MediaPlayer.create(Sayfa2.this, R.raw.b21);
                sesb1.start();
                //sesbilgisi();
            }
            return true;
        }
    });

    b7 = (ImageButton) findViewById(R.id.b7);
    b7.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {

            if (event.getAction() == MotionEvent.ACTION_DOWN) {

                /* if (sesb1 != null) {
                    sesb1.stop();
                    sesb1.release();
                }*/
                sesb1 = MediaPlayer.create(Sayfa2.this, R.raw.b31);
                sesb1.start();
                //sesbilgisi();
            }
            return true;
        }
    });

    b8 = (ImageButton) findViewById(R.id.b8);
    b8.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {

            if (event.getAction() == MotionEvent.ACTION_DOWN) {

                /* if (sesb1 != null) {
                    sesb1.stop();
                    sesb1.release();
                }*/
                sesb1 = MediaPlayer.create(Sayfa2.this, R.raw.b41);
                sesb1.start();
            }
            return true;
        }
    });

    b9 = (ImageButton) findViewById(R.id.b9);
    b9.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {

            if (event.getAction() == MotionEvent.ACTION_DOWN) {

             /* if (sesb1 != null) {
                    sesb1.stop();
                    sesb1.release();
                }*/
                sesb1 = MediaPlayer.create(Sayfa2.this, R.raw.fx1);
                sesb1.start();
            }
            return true;
        }
    });

    b10 = (ImageButton) findViewById(R.id.b10);
    b10.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {

            if (event.getAction() == MotionEvent.ACTION_DOWN) {

                /* if (sesb1 != null) {
                    sesb1.stop();
                    sesb1.release();
                }*/
                sesb1 = MediaPlayer.create(Sayfa2.this, R.raw.hihat1);
                sesb1.start();
            }
            return true;
        }
    });

    b11 = (ImageButton) findViewById(R.id.b11);
    b11.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {

            if (event.getAction() == MotionEvent.ACTION_DOWN) {

               /* if (sesb1 != null) {
                    sesb1.stop();
                    sesb1.release();
                }*/
                sesb1 = MediaPlayer.create(Sayfa2.this, R.raw.kick1);
                sesb1.start();
            }
            return true;
        }
    });

    b12 = (ImageButton) findViewById(R.id.b12);
    b12.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {

            if (event.getAction() == MotionEvent.ACTION_DOWN) {

                /* if (sesb1 != null) {
                    sesb1.stop();
                    sesb1.release();
                }*/
                sesb1 = MediaPlayer.create(Sayfa2.this, R.raw.snare1);
                sesb1.start();
            }
            return true;
        }
    });

    ///////////// SES CALMALAR BURADA OLACAK //////////////////

}

public void sesbilgisi(){

    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
            alertDialogBuilder
            .setTitle("Delete entry")
            .setMessage(""+sesb1.getAudioSessionId())

            // Specifying a listener allows you to take an action before dismissing the dialog.
            // The dialog is automatically dismissed when a dialog button is clicked.
            .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    // Continue with delete operation
                }
            })

            // A null listener allows the button to dismiss the dialog and take no further action.
            .setNegativeButton(android.R.string.no, null)
            .setIcon(android.R.drawable.ic_dialog_alert)
            .show();
}
[you can see picture of design in this link][1]}

首先你需要检查颜色是否相同。

True 或 False 布尔值可以工作,但您需要编写大量代码。

我会推荐 MediaPlayer 的不同实例,例如 mediaplayer1 和 mediaplayer2。

同一 MediaPlayer 实例上的相同颜色 运行。使用 isPlaying() 检查当前是否正在播放相同的颜色。如果为真则什么都不做,如果为假则启动它。你会弄明白的。

如果您的 audi 文件始终相同,SoundPool Class 也可以提供帮助。 SoundPool 需要的代码比您的示例中的代码少得多,而且更可靠。