Domanda

How to make the mediaplayer to stop playing sound before playing the next sound. So the sounds won't go through each other?

mp=MediaPlayer.create(this, R.raw.hekler);
ImageButton btn1 = (ImageButton) findViewById(R.id.btn1);
btn1.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
    if (mp.isPlaying()){
        mp.pause();
        mp.seekTo(0);
    }
    else{
        mp.start();
    }
    }

});

 mp2=MediaPlayer.create(this, R.raw.uzi);
 ImageButton btn2 = (ImageButton) findViewById(R.id.btn2);
 btn2.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        if (mp2.isPlaying()){
            mp2.pause();
            mp2.seekTo(0);
        }
        else{
            mp2.start();
        }
        }

 });
 mp3=MediaPlayer.create(this, R.raw.kalas);
 ImageButton btn3 = (ImageButton) findViewById(R.id.btn3);
 btn3.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        if (mp3.isPlaying()){
            mp3.pause();
            mp3.seekTo(0);
        }
        else{
            mp3.start();
        }   
    }
});
 mp4=MediaPlayer.create(this, R.raw.emka);
 ImageButton btn4 = (ImageButton) findViewById(R.id.btn4);
 btn4.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        if (mp4.isPlaying()){
            mp4.pause();
            mp4.seekTo(0);
        }
        else{
            mp4.start();
        }   
    }
});
 mp5=MediaPlayer.create(this, R.raw.uzi);
 ImageButton btn5 = (ImageButton) findViewById(R.id.btn5);
 btn5.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        if (mp5.isPlaying()){
            mp5.pause();
            mp5.seekTo(0);
        }
        else{
            mp5.start();
        }   
    }


});

 mp6=MediaPlayer.create(this, R.raw.sporet); 
 ImageButton btn6 = (ImageButton) findViewById(R.id.btn6);
 btn6.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        if (mp6.isPlaying()){
            mp6.pause();
            mp6.seekTo(0);
        }
        else{
            mp6.start();
        }   
    }
});

 } 

I've tried to stop Mp in every thread but it didn't work. Do I need something else like sound pool or there is a way to do this?

È stato utile?

Soluzione

The key is to use only one instance of MediaPlayer, when you want to play another sound, call sotp, then release, then set the mediaplayer variable to another create instance and play your next sound.

MediaPlayer mp = MediaPlayer.create(PlaySound.this, R.raw.mySound);
mp.start();

//then when you want to play another sound

mp.stop();
mp.release();
mp = null;

//then make mp equal another create and play it

EDIT: use this code

mp=MediaPlayer.create(this, R.raw.hekler);
ImageButton btn1 = (ImageButton) findViewById(R.id.btn1);
btn1.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
    if (mp.isPlaying()){
        stopPlaying(mp);
    }
    else{
        mp.start();
    }
    }

});

 } 

Do that for each of your buttons and define the stopPlaying method like so...

public void stopPlaying(MediaPlayer mp) {
    mp.stop();
    mp.release();
    mp = null;
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top