简体   繁体   中英

How to mute the sound in the button

I am new to java and I don't know what to do. This is the code that I am trying to do, what I need is to mute the sound effects that are playing to other buttons using another button (playstop) and to also unmute the effects using the same button (playstop).

MediaPlayer audio1, audio2, audio3;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    audio1 = MediaPlayer.create(MainActivity.this, R.raw.addlife);
    audio2 = MediaPlayer.create(MainActivity.this, R.raw.freeeze);
    audio3 = MediaPlayer.create(MainActivity.this, R.raw.highlight);

    Button addlife = (Button) findViewById(R.id.button);
    Button freeze = (Button) findViewById(R.id.button2);
    Button highlight = (Button) findViewById(R.id.button3);
    Button playstop = (Button) findViewById(R.id.button4);

    addlife.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            audio1.start();
            Button button = (Button) v;
            button.setVisibility(View.INVISIBLE);
        }
    });

    freeze.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            audio2.start();
            Button button = (Button) v;
            button.setVisibility(View.INVISIBLE);
        }
    });

    highlight.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            audio3.start();
            Button button = (Button) v;
            button.setVisibility(View.INVISIBLE);

        }
    });

    i = 0;

    playstop.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
        
            if (i == 0) {
                audio1.stop();
                audio2.stop();
                audio3.stop();
                playstop.setBackgroundResource(R.drawable.mute);
                i++;
            }
            else if (i == 1){
                audio1.start(); //does not work
                audio2.start(); //does not work
                audio3.start(); //does not work
                playstop.setBackgroundResource(R.drawable.unmute);
                i = 0;
            }
        }
    });

}

If you are trying to find which sound is playing right now then MediaPlayer has method - isPlaying(). It returns true if it is playing right now. In this case you need something like this:

    if (audio1.isPlaying()){
     audio1.stop()
   addLife.setVisibility(View.VISIBLE)
    }

If you need to pause/play currently playing sound you can use pause() and start() methods, but in this case you better to make some additional class to handle all manipulations. Example:

    import android.media.MediaPlayer;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.Objects;
    import java.util.UUID;
    
    public class MediaPlayerManager {
        
        public final HashMap<String, MediaPlayer> mediaPlayerList;
        public final ArrayList<String> keys;
        private String pausedPlayerKey;
        
        public MediaPlayerManager(MediaPlayer... mediaPlayers) {
            mediaPlayerList = new HashMap<>();
            keys = new ArrayList<>();
            if (mediaPlayers != null && mediaPlayers.length > 0) {
                for (MediaPlayer mediaPlayer : mediaPlayers) {
                    String uid = UUID.randomUUID().toString();
                    keys.add(uid);
                    mediaPlayerList.put(uid, mediaPlayer);
                }
            }
        }

 public boolean isAnyOnePlaying(){
        for (String key : keys) {
            MediaPlayer player = mediaPlayerList.get(key);
            if (player != null && player.isPlaying()) {
               return true;
            }
        }
        return false;
    }
    
        public void pauseIfPlaying() {
            for (String key : keys) {
                MediaPlayer player = mediaPlayerList.get(key);
                if (player != null && player.isPlaying()) {
                    player.pause();
                    pausedPlayerKey = key;
                    break;
                }
            }
        }
    
        public void resume() {
            if (pausedPlayerKey != null) {
                if (mediaPlayerList.containsKey(pausedPlayerKey) && mediaPlayerList.get(pausedPlayerKey) != null) {
                    Objects.requireNonNull(mediaPlayerList.get(pausedPlayerKey)).start();
                    pausedPlayerKey = null;
                }
            }
        }
    
    }

Then in onCreate method you can do this.

MediaPlayerManager mpm = new MediaPlayerManager({audio1, audio2, audio3});

And then:

playstop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            if (mpm.isAnyOnePlaying()){
                mpm.pauseIfPlaying();
            }else {
                mpm.resume();
            }
                //help//
            }
        });

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