3
MediaPlayer instrumental = new MediaPlayer();
MediaPlayer vocal = new MediaPlayer();

I need this 2 object to work independently when I click the Button Play.

This 2 instances should play separately.

The vocal should have its own progress bar which will indicate the volume of its audio without affecting the volume of instrumental.

Think of this problem as running two audio with different controls for volume.

doubleDown
  • 8,048
  • 1
  • 32
  • 48
ceosilvajr
  • 185
  • 1
  • 2
  • 10

2 Answers2

2

You can control the MediaPlayer's audio using its setVolume() method. Example:

MediaPlayer mp = new MediaPlayer();
...
mp.setVolume(1, 1);

The parameters are for left and right sound. To modify these values, try calculating the values as mentioned in this post: https://stackoverflow.com/a/12075910/582083

Community
  • 1
  • 1
chrisbjr
  • 628
  • 4
  • 10
1

This can be done by setting seperate audio stream types on your MediaPlayer instances. I don't know if this will have any unintended consequences, I guess you are supposed to use STREAM_MUSIC for music... but it works.

seek1 = (SeekBar) findViewById(R.id.seek1);
seek2 = (SeekBar) findViewById(R.id.seek2);

seek1.setOnSeekBarChangeListener(this);
seek2.setOnSeekBarChangeListener(this);

am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

seek1.setMax(am.getStreamMaxVolume(AudioManager.STREAM_MUSIC));
seek2.setProgress(am.getStreamMaxVolume(AudioManager.STREAM_VOICE_CALL));

// Set up your MediaPlayers
// Call the following lines before onPrepare()

instrumental.setAudioStreamType(AudioManager.STREAM_MUSIC);
vocal.setAudioStreamType(AudioManager.STREAM_VOICE_CALL);

Then later in your onSeekBarChangeListener

@Override
public void onStopTrackingTouch(SeekBar seekBar) {
    if (seekBar.equals(seek1)) {
        am.setStreamVolume(AudioManager.STREAM_MUSIC, seekBar.getProgress(), 0);
    }
    else {
        am.setStreamVolume(AudioManager.STREAM_VOICE_CALL, seekBar.getProgress(), 0);
    }

}
Ken Wolf
  • 23,133
  • 6
  • 63
  • 84
  • Thanks for this Sir ken, but What I'm trying to do is to adjust the volume of vocal which is another audio file in the assets. – ceosilvajr Jul 03 '13 at 09:13
  • 1
    This will do that. I have it working on my phone right now. With 2 audio files from assets. And 2 sliders controlling 2 separate streams. Have you tried it? What's the problem? – Ken Wolf Jul 03 '13 at 09:14
  • Is it safe to use the AudioManager.STREAM_VOICE_CALL? because together with this problem I have a recording function which will record my voice. while playing this two audios. – ceosilvajr Jul 03 '13 at 09:37
  • 1
    As far as I can tell it's safe. I don't know how else you can control 2 different volume streams. You will need different streams. You could try a different one. ALARM, etc. – Ken Wolf Jul 03 '13 at 09:38