如何在另一个音频文件开始播放时暂停音频文件,反之亦然

How to pause one audio file when another starts playing and viseversa

我有两个音频文件。我希望如果一个音频正在播放并且用户单击第二个音频,则第一个音频将暂停(同时保持进度并在再次播放时继续播放)并开始第二个音频,反之亦然。

如何实现此功能?

我是 Android 媒体的新手

代码

onClick在最后,如果你想直接跳转到XML,onClick在最后实现,如果你想查看XML请告诉我,我会更新它。

public class MainActivity extends AppCompatActivity {
    MediaPlayer player1, player2;
    SeekBar seekBar1, seekBar2;
    TextView currentTime1, currentTime2;
    TextView remainingTime1, remainingTime2;
    ImageView play1, play2;
    int totalTime1, totalTime2;

    @Override

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

        // PlayButton    *  The onClick is in the last if you want to jump directly there  *

        play1 = findViewById(R.id.playbtn);
        play2 = findViewById(R.id.playbtn2);

        // TimeLables

        currentTime1 = findViewById(R.id.currentTime1);
        currentTime2 = findViewById(R.id.currentTime2);
        remainingTime1 = findViewById(R.id.totalTime1);
        remainingTime2 = findViewById(R.id.totalTime2);

        // MediaPlayer

        player1 = MediaPlayer.create(this, R.raw.dog_howl);
        player2 = MediaPlayer.create(this, R.raw.dog_bark);
        player1.setLooping(false);
        player1.seekTo(0);
        totalTime1 = player1.getDuration();
        player2.setLooping(false);
        player2.seekTo(0);
        totalTime2 = player2.getDuration();


        //SeekBar *Progress*

        seekBar1 = findViewById(R.id.seekbar1);
        seekBar2 = findViewById(R.id.seekbar2);
        seekBar1.setMax(totalTime1);
        seekBar2.setMax(totalTime2);

        seekBar1.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                player1.seekTo(progress);
                seekBar1.setProgress(progress);

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {


            }
        });
        seekBar2.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                player2.seekTo(i);
                seekBar2.setProgress(i);


            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {


            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {


            }
        });


        new Thread(() -> {
            while (player1 != null) {
                try {
                    Message msg = new Message();
                    msg.what = player1.getCurrentPosition();
                    handler1.sendMessage(msg);
                    Thread.sleep(1000000000);
                } catch (InterruptedException ignored) {

                }
            }
        }).start();

        new Thread(() -> {
            while (player2 != null) {
                try {
                    Message msg = new Message();
                    msg.what = player2.getCurrentPosition();
                    handler2.sendMessage(msg);
                    Thread.sleep(1000000000);
                } catch (InterruptedException ignored) {

                }
            }
        }).start();


        // Admob Banner Ad

        MobileAds.initialize(this, initializationStatus -> {
        });

        AdView mAdView = findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();
        mAdView.loadAd(adRequest);

    }

    @SuppressLint("HandlerLeak")
    private final Handler handler1 = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            int currentPosition1 = msg.what;
            //update seekbar
            seekBar1.setProgress(currentPosition1);
            currentTime1.setText(createTimerLable1(seekBar1.getProgress()));
            String totTime1 = createTimerLable1(player1.getDuration());
            remainingTime1.setText(totTime1);

        }
    };
    @SuppressLint("HandlerLeak")
    private final Handler handler2 = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            int currentPosition2 = msg.what;
            //update seekbar
            seekBar2.setProgress(currentPosition2);
            currentTime2.setText(createTimerLable1(seekBar2.getProgress()));
            String totTime2 = createTimerLable2(player2.getDuration());
            remainingTime2.setText(totTime2);

        }
    };

    public String createTimerLable1(int duration) {
        String timerLabel = "";
        int min = duration / 1000 / 60;
        int sec = duration / 1000 % 60;
        timerLabel += min + ":";
        if (sec < 10) timerLabel += "0";
        timerLabel += sec;
        return timerLabel;


    }

    public String createTimerLable2(int duration) {
        String timerLabel = "";
        int min = duration / 1000 / 60;
        int sec = duration / 1000 % 60;
        timerLabel += min + ":";
        if (sec < 10) timerLabel += "0";
        timerLabel += sec;
        return timerLabel;


    }

    public void playBtnClick1(View view) {
        if (!player1.isPlaying()) {
            // Stoping
            player1.start();
            play1.setImageResource(R.drawable.ic_baseline_pause_circle_filled_24);
        } else {
            // Playing
            player1.pause();
            play1.setImageResource(R.drawable.ic_baseline_play_circle_filled_24);
        }

    }

    public void playBtnClick2(View view) {
        if (!player2.isPlaying()) {
            // Stoping
            player2.start();
            play2.setImageResource(R.drawable.ic_baseline_pause_circle_filled_24);
        } else {
            // Playing
            player2.pause();
            play2.setImageResource(R.drawable.ic_baseline_play_circle_filled_24);
        }

    }

}


// TimeLable issue  * please read the comments for whats happening here *

public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                player2.seekTo(i);
                seekBar2.setProgress(i);
                currentTime2.setText(createTimerLable2(i));


            }

添加此代码:-

public void playBtnClick1(View view) {

    if (player2.isPlaying()) {
        player2.pause();
        play2.setImageResource(R.drawable.ic_baseline_play_circle_filled_24);
    }
    
    if (!player1.isPlaying()) {
        // Stoping
        player1.start();
        play1.setImageResource(R.drawable.ic_baseline_pause_circle_filled_24);
    } else {
        // Playing
        player1.pause();
        play1.setImageResource(R.drawable.ic_baseline_play_circle_filled_24);
    }

}

public void playBtnClick2(View view) {

    if (player1.isPlaying()) {
        player1.pause();
        play1.setImageResource(R.drawable.ic_baseline_play_circle_filled_24);
    }
    
    if (!player2.isPlaying()) {
        // Stoping
        player2.start();
        play2.setImageResource(R.drawable.ic_baseline_pause_circle_filled_24);
    } else {
        // Playing
        player2.pause();
        play2.setImageResource(R.drawable.ic_baseline_play_circle_filled_24);
    }

}

如果我明白您的意思,您的解决方案真的很简单。 当你想玩新的时候你可以停止上一个。

只需使用这两种方法:

    player1.start();
    player2.stop();

播放器 2 开始停止播放器 1。