Problems with Media Player

2019-07-05 05:29发布

问题:

Alright so, I am making sound application for android and there is a slight problem. I made my app to stop playing current sound when I click on another button to play another sound, so they don't play on top of each other. For first 4 Media Players, it's working good. But there is a problem on 5th and 6th Media player and I can't figure out what's causing it. When I play Mp6, and I want to play for example Mp2, it pauses it first so I need to double click to play it. heres my 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()){
        mp.pause();
        mp.seekTo(0);
    }
    if (mp2.isPlaying()){
        mp2.pause();
        mp2.seekTo(0);
    }
    if (mp3.isPlaying()){
        mp3.pause();
        mp3.seekTo(0);
    }
    if (mp4.isPlaying()){
        mp4.pause();
        mp4.seekTo(0);
    }
    if (mp5.isPlaying()){
        mp5.pause();
        mp5.seekTo(0);

    }
    if (mp6.isPlaying()){
        mp6.pause();
        mp6.seekTo(0);

    }
    else{
        mp.start();
    }
    }

});

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

    @Override
    public void onClick(View v) {
        if (mp.isPlaying()){
            mp.pause();
            mp.seekTo(0);
        }
        if (mp3.isPlaying()){
            mp3.pause();
            mp3.seekTo(0);
        }
        if (mp4.isPlaying()){
            mp4.pause();
            mp4.seekTo(0);
        }
        if (mp5.isPlaying()){
            mp5.pause();
            mp5.seekTo(0);

        }
        if (mp6.isPlaying()){
            mp6.pause();
            mp6.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);
        }
        if (mp.isPlaying()){
            mp.pause();
            mp.seekTo(0);
        }
        if (mp2.isPlaying()){
            mp2.pause();
            mp2.seekTo(0);
        }

        if (mp4.isPlaying()){
            mp4.pause();
            mp4.seekTo(0);
        }
        if (mp5.isPlaying()){
            mp5.pause();
            mp5.seekTo(0);

        }
        if (mp6.isPlaying()){
            mp6.pause();
            mp6.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);
        }
        if (mp.isPlaying()){
            mp.pause();
            mp.seekTo(0);
        }
        if (mp2.isPlaying()){
            mp2.pause();
            mp2.seekTo(0);
        }
        if (mp3.isPlaying()){
            mp3.pause();
            mp3.seekTo(0);
        }
        if (mp5.isPlaying()){
            mp5.pause();
            mp5.seekTo(0);

        }
        if (mp6.isPlaying()){
            mp6.pause();
            mp6.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 (mp.isPlaying()){
            mp.pause();
            mp.seekTo(0);
        }
        if (mp2.isPlaying()){
            mp2.pause();
            mp2.seekTo(0);
        }
        if (mp3.isPlaying()){
            mp3.pause();
            mp3.seekTo(0);
        }
        if (mp4.isPlaying()){
            mp4.pause();
            mp4.seekTo(0);
        }

        if (mp6.isPlaying()){
            mp6.pause();
            mp6.seekTo(0);
        }
        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 (mp.isPlaying()){
                mp.pause();
                mp.seekTo(0);
            }
            if (mp2.isPlaying()){
                mp2.pause();
                mp2.seekTo(0);
            }
            if (mp3.isPlaying()){
                mp3.pause();
                mp3.seekTo(0);
            }

            if (mp4.isPlaying()){
                mp4.pause();
                mp4.seekTo(0);
            }

            if (mp5.isPlaying()){
                mp5.pause();
                mp5.seekTo(0);
            }
            if (mp6.isPlaying()){
                mp6.pause();
                mp6.seekTo(0);
            }

            else{
                mp6.start();
            }   
        }
    });

}

回答1:

This time, the problem is with your order of checking the media players and logical if else conflict.

Make use of this code in your mp2 part:

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

@Override
public void onClick(View v) {

    if (mp.isPlaying()){
        mp.pause();
        mp.seekTo(0);
    }
    if (mp3.isPlaying()){
        mp3.pause();
        mp3.seekTo(0);
    }
    if (mp4.isPlaying()){
        mp4.pause();
        mp4.seekTo(0);
    }
    if (mp5.isPlaying()){
        mp5.pause();
        mp5.seekTo(0);

    }
    if (mp6.isPlaying()){
        mp6.pause();
        mp6.seekTo(0);
    }
     if (mp2.isPlaying()){ // Put this if branch just before else, likewise for other media players too.
        mp2.pause();
        mp2.seekTo(0);
    } 
    else{
        mp2.start();
    }
    }

});

Always check if mp2 is playing or not just before the else condition. Likewise for others. That is, only after do you check other players, you should check the current player you are trying to play.

Let me explain why in detail:

 if (mp6.isPlaying()){
        mp6.pause();
        mp6.seekTo(0);
    }

    else{
        mp2.start();
    }
    }

In that portion of your code in mp2 part, you are checking if mp6 is playing and stopping it. Since if holds good, else won't execute in that turn. Hence you have to press again to make mp2 play. By doing as I said, that is, checking the current player will be matched with the corresponding else condition, and hence your problem will be solved.

EDIT: In all six button click events, you have checked if mp6 is playing just before the else part. This is a logical flaw. Before the else, you should have the if of the corresponding mediaplayer object.