Я пытаюсь передать один аудиофайл для фоновой музыки.Скажем, если пользователь нажмет кнопку, он будет воспроизводить другой аудиофайл при понижении фоновой музыки.Я пытался использовать AudioManager
, но он изменяет оба тома.
А вот мой файл Java:
public static void main(Context contex){
//set the background music, first audio file
//start the music when the activity starts
MediaPlayer mediaplayer_background = new MediaPlayer().create(context, R.raw.background);
/*
audioManager_backgroud.setStreamVolume(
AudioManager.STREAM_RING,
//mAudioManager.getStreamMaxVolume(AudioManager.STREAM_RING),
10,
0
);*/
mediaplayer_background.start();
//set the button music, second audio file
MediaPlayer mediaplayer_button = new MediaPlayer().create(context, R.raw.clicksound);
//Set the button on touch listener
//if the user touch the button, it will play
binding.button1.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// if the user stop touching the button, the audio file for this button will stop
if (event.getAction() == MotionEvent.ACTION_UP) {
if (mediaplayer_button.isPlaying() == true) {
mediaplayer_button.pause();
}
//When the user touch the button, change the color of the button to the gray
//start the second audio file
} else if(event.getAction() == MotionEvent.ACTION_DOWN){
binding.button1.setBackgroundColor(Color.LTGRAY);
if (mediaplayer_button.isPlaying() == false) {
mediaplayer_button.start();
}
}
return false;
}
});
}