我无法在每个MediaPlayer上分隔音量。我需要做的就是mp1应该有自己的音量控制以及mp2。代码如下所示。我甚至尝试在每个MediaPlayer上创建一个setVolume()但没有运气!
seekBar1和seekBar2卷都适用于mp1和mp2。我只需将它们分开,seekBar1应仅适用于mp1。
公共类MainActivity扩展了AppCompatActivity {
MediaPlayer mp1;
MediaPlayer mp2;
AudioManager audioManager1;
AudioManager audioManager2;
...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mp1 = MediaPlayer.create(this, R.raw.audio1);
mp2 = MediaPlayer.create(this, R.raw.audio1);
audioManager1 = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
audioManager2 = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
int maxVolume1 = audioManager1.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
int curVolume1 = audioManager1.getStreamVolume(AudioManager.STREAM_MUSIC);
int maxVolume2 = audioManager2.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
int curVolume2 = audioManager2.getStreamVolume(AudioManager.STREAM_MUSIC);
SeekBar volumeControl1 = (SeekBar) findViewById(R.id.seekBar1);
SeekBar volumeControl2 = (SeekBar) findViewById(R.id.seekBar2);
volumeControl1.setMax(maxVolume1);
volumeControl2.setMax(maxVolume2);
volumeControl1.setProgress(curVolume1);
volumeControl2.setProgress(curVolume2);
volumeControl1.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean b) {
audioManager1.setStreamVolume(AudioManager.STREAM_MUSIC, progress, 0);
}
});