How to adjust the volume of two media players simultaneously?

1k views Asked by At

I am running two media players simultaneously to play two different sounds, one is audio recording and second one is music. I am controlling the volume of both through two seekbars. The problem is, volume of one media player is controlled at once. When one media player is released, volume of other media player can be adjusted. I want to adjust SIMULTANEOUSLY Here's my implementation.

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

In seekbars, i am using audio manager to adjust volumes. In music seekbar, volume is adjusted through,

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

Code for changing the volume of recording seekbar is,

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

There are 1 answers

0
Behram Sardar On BEST ANSWER

Here's the solution to play 2 media players simultaneously and control their volume through seekbars.

Create Variables

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";

Function to intialize and start media players:

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

}

Create SeekBarChangeListener to get the current progress.

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) {

    }
};

In activity or fragment

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