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
Here's the solution to play 2 media players simultaneously and control their volume through seekbars.
Create Variables
Function to intialize and start media players:
Create SeekBarChangeListener to get the current progress.
In activity or fragment