开始播放其他音频时如何停止媒体播放器?

时间:2019-07-25 21:26:15

标签: java android android-studio android-mediaplayer

我正在尝试开发应用程序。此应用程序中有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]}

1 个答案:

答案 0 :(得分:0)

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

正确或错误的布尔值都可以工作,但是您需要编写大量代码。

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

相同的颜色在MediaPlayer的同一实例上运行。使用isPlaying()检查当前是否正在播放相同的颜色。如果为true,则什么也不做,如果为false,则将其启动。您会发现的。

如果您的audi文件始终相同,则SoundPool类也可以提供帮助。 SoundPool所需的代码要比样本中的代码少得多,并且更可靠。