防止Media Player同时播放多个声音

时间:2014-01-03 10:00:14

标签: android audio media-player playback

如何在播放下一个声音之前让媒体播放器停止播放声音。所以声音不会相互通过?

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();
        }   
    }
});

 } 

我试图在每个帖子中停止Mp,但它没有用。我需要其他类似声音池的东西,还是有办法做到这一点?

1 个答案:

答案 0 :(得分:2)

关键是只使用一个MediaPlayer实例,当你想播放另一个声音,调用sotp,然后释放,然后将mediaplayer变量设置为另一个创建实例并播放下一个声音。

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

编辑:使用此代码

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();
    }
    }

});

 } 

为每个按钮执行此操作并定义 stopPlaying 方法,如此...

public void stopPlaying(MediaPlayer mp) {
    mp.stop();
    mp.release();
    mp = null;
}