如何同时调节两个媒体播放器的音量?

How to adjust the volume of two media players simultaneously?

我是运行两个媒体播放器同时播放两种不同的声音,一种是录音,一种是音乐。我通过两个搜索栏控制两者的音量。问题是,一次控制一个媒体播放器的音量。当释放一个媒体播放器时,可以调节其他媒体播放器的音量。我想同时调整 这是我的实现。

 public void setMediaPLayers() {
    mediaPlayer = new MediaPlayer(); //TO PLAY VOICE RECORDING
    mediaPlayer.setAudioStreamType(AudioManager.STREAM_VOICE_CALL);

    try {
        mediaPlayer.setDataSource(AudioSavePathInDevice); //VOICE RECORDING FILE PATH
        mediaPlayer.prepare();
        mediaPlayer.start();

    } catch (IOException e) {
        e.printStackTrace();
    }
    mediaPlayer2 = MediaPlayer.create(getActivity(), R.raw.sunnysideup); //FOR MUSIC FILE
    mediaPlayer2.setAudioStreamType(AudioManager.STREAM_MUSIC);
    mediaPlayer2.start();
}

在搜索栏中,我正在使用音频管理器来调整音量。在音乐搜索栏中,通过

调节音量
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, i, 0); // i = seekbar progress

改变录音seekbar音量的代码是,

audioManager.setStreamVolume(AudioManager.STREAM_VOICE_CALL, i, 0); // i = seekbar progress

这是同时播放 2 个媒体播放器并通过搜索栏控制其音量的解决方案。

创建变量

MediaPlayer mpOne = null;
MediaPlayer mpTwo = null;
SeekBar seekBarOne, seekBarTwo;

//Path to mp3 file stored in device
String FILE_PATH_ONE = Environment.getExternalStorageDirectory().getPath() + "/music_one.mp3";
String FILE_PATH_TWO = Environment.getExternalStorageDirectory().getPath() + "/music_two.mp3";

初始化和启动媒体播放器的函数:

public void setMediaPlayers() {
    mpOne = new MediaPlayer();
    mpTwo = new MediaPlayer();


    try {
        mpOne.setDataSource(FILE_PATH_ONE);
        mpTwo.setDataSource(FILE_PATH_TWO);
        mpOne.prepare();
        mpTwo.prepare();
        mpOne.start();
        mpTwo.start();

    } catch (IOException e) {
        e.printStackTrace();
    }

}

创建 SeekBarChangeListener 以获取当前进度。

SeekBar.OnSeekBarChangeListener seekBarOneListener = new SeekBar.OnSeekBarChangeListener() {

    @Override
    public void onProgressChanged(SeekBar seekBar, int i, boolean b) {

        //Lets assume, seekbar's maximum progress is 10.
        //i is the progress of seekbar between 0 to 10.

        float volume = i * 0.10f;

        //Set the volume if media player is  not null
        if (null != mpOne)
            //volume is the float number between 0.0 and 1.0
            mpOne.setVolume(volume, volume);
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {

    }
};

SeekBar.OnSeekBarChangeListener seekBarTwoListener = new SeekBar.OnSeekBarChangeListener() {

    @Override
    public void onProgressChanged(SeekBar seekBar, int i, boolean b) {

        //Lets assume, seekbar's maximum progress is 10.
        //i is the progress of seekbar between 0 to 10.

        float volume = i * 0.10f;

        //Set the volume if media player is  not null
        if (null != mpTwo)
            //volume is the float number between 0.0 and 1.0
            mpTwo.setVolume(volume, volume);
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {

    }
};

在activity或片段

seekBarOne = (SeekBar) v.findViewById(R.id.seekBar1);
seekBarTwo = (SeekBar) v.findViewById(R.id.seekBar2);

seekBarOne.setOnSeekBarChangeListener(seekBarOneListener);
seekBarTwo.setOnSeekBarChangeListener(seekBarTwoListener);

//Start media players
setMediaPlayers();

//Completion listeners for releasing media players on completion.
mpOne.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mp) {

            mpOne.reset();
            mpOne.release();
            mpOne = null;
        }
    });
 mpTwo.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mp) {

            mpTwo.reset();
            mpTwo.release();
            mpTwo = null;
        }
    });