简体   繁体   中英

Get MediaPlayer to stop playing

I'm trying to make the mediaplayer to stop playing sound before playing the next sound. So the sounds won't go through each other. It's my first app, and I thought I should start by making a soundboard.

This is what I have now:

    final MediaPlayer mediaPlayer = new MediaPlayer();
    // import sound files
    final MediaPlayer sound01 = MediaPlayer.create(this, R.raw.sound01);
    final MediaPlayer sound02 = MediaPlayer.create(this, R.raw.sound02);
    final MediaPlayer sound03 = MediaPlayer.create(this, R.raw.sound03);
    final MediaPlayer sound04 = MediaPlayer.create(this, R.raw.sound04);
    final MediaPlayer sound05 = MediaPlayer.create(this, R.raw.sound05);
    final MediaPlayer sound06 = MediaPlayer.create(this, R.raw.sound06);
    final MediaPlayer sound07 = MediaPlayer.create(this, R.raw.sound07);
    final MediaPlayer sound08 = MediaPlayer.create(this, R.raw.sound08);
    final MediaPlayer sound09 = MediaPlayer.create(this, R.raw.sound09);
    final MediaPlayer sound10 = MediaPlayer.create(this, R.raw.sound10);
    final MediaPlayer sound11 = MediaPlayer.create(this, R.raw.sound11);
    final MediaPlayer sound12 = MediaPlayer.create(this, R.raw.sound12);
    final MediaPlayer sound13 = MediaPlayer.create(this, R.raw.sound13);
    final MediaPlayer sound14 = MediaPlayer.create(this, R.raw.sound14);
    final MediaPlayer sound15 = MediaPlayer.create(this, R.raw.sound15);
    final MediaPlayer sound16 = MediaPlayer.create(this, R.raw.sound16);
    final MediaPlayer sound17 = MediaPlayer.create(this, R.raw.sound17);
    final MediaPlayer sound18 = MediaPlayer.create(this, R.raw.sound18);
    final MediaPlayer sound19 = MediaPlayer.create(this, R.raw.sound19);
    final MediaPlayer sound20 = MediaPlayer.create(this, R.raw.sound20);
    final MediaPlayer sound21 = MediaPlayer.create(this, R.raw.sound21);
    final MediaPlayer sound22 = MediaPlayer.create(this, R.raw.sound22);
    final MediaPlayer sound23 = MediaPlayer.create(this, R.raw.sound23);
    final MediaPlayer sound24 = MediaPlayer.create(this, R.raw.sound24);
    final MediaPlayer sound25 = MediaPlayer.create(this, R.raw.sound25);


    // play sound files on clicks
    Button s01 = (Button) findViewById(R.id.btn1_1); 
    s01.setText(this.getString(R.string.button1_1));
    s01.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            try {
                sound01.prepare();
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            mediaPlayer.stop();
            sound01.start();                
            }

    });
    registerForContextMenu(s01);

Thanks in advance!

You only need one MediaPlayer; you definitely don't need to load multiple instances. Each button will call sound.stop(); and then load the next sound.

No. You've created 25 different instances of MediaPlayer. There is no static method to stop playing. The best way is to keep track yourself of which one is currently playing, and just stop the one when required.

Actually, for this purpose, you're probably much better off using SoundPool , which would give you one object instance to manage all of your sounds, instead of one player per sound.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM