简体   繁体   中英

How to stop playing sounds on button click to start another sound file?

I have two buttons and two songs. If I click the first button, the first sound plays. But if I click the second button while the first sound is playing the second sound starts playing too.
How can I stop other sounds?

My code:

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

    final MediaPlayer johnCenaPlayer = MediaPlayer.create(this, R.raw.john_cena);
    Button johnCenaButton = (Button) findViewById(R.id.john_cena_button);
    johnCenaButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            johnCenaPlayer.start();
        }
    });

    final MediaPlayer haGayPlayer = MediaPlayer.create(this, R.id.ha_gay_button);
    Button haGayButton = (Button) findViewById(R.id.ha_gay_button);
    haGayButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            haGayPlayer.start();
        }
    });
}

Stop the other MediaPlayer in clickListener using stop() method.

public void onClick(View view) {
    ha_gay.stop()
    john_cena.start();
}

If you have many audio files use a single MediaPlayer and change the sources dynamically.

public class MainActivity extends AppCompatActivity {


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

    final MediaPlayer mediaPlayer = new MediaPlayer();

    Button john_cena_button = (Button) findViewById(R.id.john_cena_button);
    john_cena_button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            stopAndPlay(R.raw.john_cena, mediaPlayer);
        }
    });

    Button ha_gay_button = (Button) findViewById(R.id.ha_gay_button);
    ha_gay_button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            stopAndPlay(R.raw.ha_gay, mediaPlayer);
        }
    });
}

// This resets the mediaPlayer and starts the given audio
private void stopAndPlay(int rawId, MediaPlayer mediaPlayer) {
    mediaPlayer.reset();
    AssetFileDescriptor afd = this.getResources().openRawResourceFd(rawId);
    try {
        mediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
        mediaPlayer.prepare();
    } catch (IOException e) {
        e.printStackTrace();
    }
    mediaPlayer.start();
}

}

If you have multiple audio files, I think an easy solution would be to assign each of the buttons created to play your audio file, a tag.The use of tags makes it much easier to start, and stop your button. When using tags, you should set the tag equal to the file name. Once you do that, here is the actual code that you can use:

     public void play(View view){

    buttonPressed = (Button) view;

    mp = MediaPlayer.create(this, getResources().getIdentifier(buttonPressed.getTag().toString(), "raw", getPackageName()));

    mp.start();

    }

Side not - Assign your media player at the very beginning of your code, so it will be easier for you to call it later on. EX:

    MediaPlayer mp; 

To stop the file from playing, try this:

    public void stop(View view){

    if(mp.isPlaying()){

    mp.stop();
    }
    }

I'm only 14, and I just started learning java, not too long ago, but with the knowledge that I have, thus far, I think my code should be an easy fix for your problem.

Try this one. Do it same for the other.

final MediaPlayer john_cena = MediaPlayer.create(this, R.raw.john_cena);
Button john_cena_button = (Button) findViewById(R.id.john_cena_button);
john_cena_button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        if (he_gay.isPlaying()){
             he_gay.stop();
    }
        else if (john_cena.isPlaying()){
            john_cena.seekTo(0);
    }  else{
       john_cena.start();
    }
}
});

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