Android MediaPlayer 运行 有重叠

Android MediaPlayer run with overlapping

感谢阅读本文 post,我开发的应用程序功能之一有问题,即我通过原始文件夹 运行 使用的 MediaPlayer。当我尝试同时 播放 2 个 MediaPlayer 时,另一个 MediaPlayer 应该 自动停止 当另一个 MediaPlayer 运行 s,但是这个不行,即使退出activity,MediaPlayer还是运行ning,请问有没有相关的解决办法。这是我的代码

    MediaPlayer mediaPlayer1,mediaPlayer2,mediaPlayer3;
    ImageButton btn1, btn2, btn3;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hukum_ra_tarqiq);

        btn1 = findViewById(R.id.play_toggle_ratarqiq1);
        btn1.setOnClickListener(this);
        mediaPlayer1 = MediaPlayer.create(this,R.raw.madtarqiqc1);
        btn2 = findViewById(R.id.play_toggle_ratarqiq2);
        btn2.setOnClickListener(this);
        mediaPlayer2 = MediaPlayer.create(this,R.raw.madtarqiqc2);
        btn3 = findViewById(R.id.play_toggle_ratarqiq3);
        btn3.setOnClickListener(this);
        mediaPlayer3 = MediaPlayer.create(this,R.raw.madtarqiqc3);

    }
@Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.play_toggle_ratarqiq1:
                setmpFirst();
                break;
            case R.id.play_toggle_ratarqiq2:
                setmpSecond();
                break;
            case R.id.play_toggle_ratarqiq3:
                setmpThird();
                break;
        }
    }
private void setmpFirst(){
        if (mediaPlayer1.isPlaying()){
            if (mediaPlayer1 != null){
                mediaPlayer1.pause();
                if (isFinishing()){
                    mediaPlayer1.stop();
                    mediaPlayer1.release();
                }
            }
            btn1.setImageResource(R.drawable.ic_pause_circle_outline_black_24dp);
        }else {
            mediaPlayer1.start();
            btn1.setImageResource(R.drawable.ic_play_circle_outline_black_24dp);
        }
    }

    private void setmpSecond(){
        if (mediaPlayer2.isPlaying()){
            if (mediaPlayer2 != null){
                mediaPlayer2.pause();
                if (isFinishing()){
                    mediaPlayer2.stop();
                    mediaPlayer2.release();
                }
            }
            btn2.setImageResource(R.drawable.ic_pause_circle_outline_black_24dp);
        }
        else {
            mediaPlayer2.start();
            btn2.setImageResource(R.drawable.ic_play_circle_outline_black_24dp);
        }
    }

    private void setmpThird(){
        if (mediaPlayer3.isPlaying()){
            if (mediaPlayer3 != null){
                mediaPlayer3.pause();
                if (isFinishing()){
                    mediaPlayer3.stop();
                    mediaPlayer3.release();
                }
            }
            btn3.setImageResource(R.drawable.ic_pause_circle_outline_black_24dp);
        }
        else {
            mediaPlayer3.start();
            btn3.setImageResource(R.drawable.ic_play_circle_outline_black_24dp);
        }
    }

我要的是,当上面的MediaPlayer运行同时时,MediaPlayer先点到运行,然后其他stop,以及我退出activity的时候,MediaPlayer自动停止,请问有没有解决办法或者从上面的代码中有没有简化一下?

制作你的播放列表。使用数组。

int[] playList = new int[3];
playList[0] = R.raw.madtarqiqc1;
playList[1] = R.raw.madtarqiqc2;
playList[2] = R.raw.madtarqiqc3;

并在 Global Variable

中设置播放位置
int play = 0;

您可以使用 mediaPlayer.setOnCompletionListener()

进行控制

例如,

MediaPlayer mediaPlayer = MediaPlayer.create(this, playList[play]);
mediaPlayer.start();
mediaPlayer.setOnCompletionListener(completionListener);
MediaPlayer.OnCompletionListener completionListener = new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mediaPlayer) {
            play++;
            if (play < playList.length) {
                mediaPlayer = MediaPlayer.create(BackGroundSound.this, playList[play]);
                mediaPlayer.start();
            } else {
                play = 0;
                mediaPlayer = MediaPlayer.create(BackGroundSound.this, playList[play]);
                mediaPlayer.start();
            }
        }
    };

以及当您创建下一个按钮(或单击事件)时。只是

mediaPlayer.stop(); 
mediaPlayer = MediaPlayer.create(this, playList[position]);
mediaPlayer.start();

p.s]我修改你的代码。检查这个。

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    MediaPlayer mediaPlayer;
    ImageButton btn1, btn2, btn3;
    int[] playList;
    int play = 0;

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

        btn1 = (ImageButton) findViewById(R.id.play_toggle_ratarqiq1);
        btn2 = (ImageButton) findViewById(R.id.play_toggle_ratarqiq2);
        btn3 = (ImageButton) findViewById(R.id.play_toggle_ratarqiq3);

        int[] playList = new int[3];
        playList[0] = R.raw.madtarqiqc1;
        playList[1] = R.raw.madtarqiqc2;
        playList[2] = R.raw.madtarqiqc3;

        mediaPlayer = MediaPlayer.create(this, playList[play]);
        mediaPlayer.setOnCompletionListener(completionListener);

    }

    MediaPlayer.OnCompletionListener completionListener = new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mediaPlayer) {
            play++;
            if (play < playList.length) {
                mediaPlayer = MediaPlayer.create(MainActivity.this, playList[play]);
                mediaPlayer.start();
            } else {
                play = 0;
                mediaPlayer = MediaPlayer.create(MainActivity.this, playList[play]);
                mediaPlayer.start();
            }
        }
    };

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.play_toggle_ratarqiq1:
                playMusic(0);
                break;
            case R.id.play_toggle_ratarqiq2:
                playMusic(1);
                break;
            case R.id.play_toggle_ratarqiq3:
                playMusic(2);
                break;
        }
    }

    private void playMusic(int position) {
        mediaPlayer.stop();
        mediaPlayer = MediaPlayer.create(this, playList[position]);
        mediaPlayer.start();

        changeView(position);
    }

    private void changeView(int position) {
        if (position == 0) {
            btn1.setImageResource(R.drawable.ic_play_circle_outline_black_24dp);
            btn2.setImageResource(R.drawable.ic_pause_circle_outline_black_24dp);
            btn3.setImageResource(R.drawable.ic_pause_circle_outline_black_24dp);
        } else if (position == 1) {
            btn1.setImageResource(R.drawable.ic_pause_circle_outline_black_24dp);
            btn2.setImageResource(R.drawable.ic_play_circle_outline_black_24dp);
            btn3.setImageResource(R.drawable.ic_pause_circle_outline_black_24dp);
        } else if (position == 2) {
            btn1.setImageResource(R.drawable.ic_pause_circle_outline_black_24dp);
            btn2.setImageResource(R.drawable.ic_pause_circle_outline_black_24dp);
            btn3.setImageResource(R.drawable.ic_play_circle_outline_black_24dp);
        }
    }

}