сделать свой список воспроизведения. используя array.
int[] playList = new int[3];
playList[0] = R.raw.madtarqiqc1;
playList[1] = R.raw.madtarqiqc2;
playList[2] = R.raw.madtarqiqc3;
и установите позицию воспроизведения в Global Variable
int play = 0;
, и вы можете управлять, используя mediaPlayer.setOnCompletionListener()
, например,
MediaPlayer mediaPlayer = MediaPlayer.create(this, playList[play]);
mediaPlayer.start();
mediaPlayer.setOnCompletionListener(completionListener);
MediaPlayer.OnCompletionListener completionListener = new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
play++;
if (play < playList.length) {
mediaPlayer = MediaPlayer.create(BackGroundSound.this, playList[play]);
mediaPlayer.start();
} else {
play = 0;
mediaPlayer = MediaPlayer.create(BackGroundSound.this, playList[play]);
mediaPlayer.start();
}
}
};
и когда вы делаете следующую кнопку (или событие щелчка). просто
mediaPlayer.stop();
mediaPlayer = MediaPlayer.create(this, playList[position]);
mediaPlayer.start();
ps] Я изменяю ваш код. проверьте это.
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
MediaPlayer mediaPlayer;
ImageButton btn1, btn2, btn3;
int[] playList;
int play = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (ImageButton) findViewById(R.id.play_toggle_ratarqiq1);
btn2 = (ImageButton) findViewById(R.id.play_toggle_ratarqiq2);
btn3 = (ImageButton) findViewById(R.id.play_toggle_ratarqiq3);
int[] playList = new int[3];
playList[0] = R.raw.madtarqiqc1;
playList[1] = R.raw.madtarqiqc2;
playList[2] = R.raw.madtarqiqc3;
mediaPlayer = MediaPlayer.create(this, playList[play]);
mediaPlayer.setOnCompletionListener(completionListener);
}
MediaPlayer.OnCompletionListener completionListener = new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
play++;
if (play < playList.length) {
mediaPlayer = MediaPlayer.create(MainActivity.this, playList[play]);
mediaPlayer.start();
} else {
play = 0;
mediaPlayer = MediaPlayer.create(MainActivity.this, playList[play]);
mediaPlayer.start();
}
}
};
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.play_toggle_ratarqiq1:
playMusic(0);
break;
case R.id.play_toggle_ratarqiq2:
playMusic(1);
break;
case R.id.play_toggle_ratarqiq3:
playMusic(2);
break;
}
}
private void playMusic(int position) {
mediaPlayer.stop();
mediaPlayer = MediaPlayer.create(this, playList[position]);
mediaPlayer.start();
changeView(position);
}
private void changeView(int position) {
if (position == 0) {
btn1.setImageResource(R.drawable.ic_play_circle_outline_black_24dp);
btn2.setImageResource(R.drawable.ic_pause_circle_outline_black_24dp);
btn3.setImageResource(R.drawable.ic_pause_circle_outline_black_24dp);
} else if (position == 1) {
btn1.setImageResource(R.drawable.ic_pause_circle_outline_black_24dp);
btn2.setImageResource(R.drawable.ic_play_circle_outline_black_24dp);
btn3.setImageResource(R.drawable.ic_pause_circle_outline_black_24dp);
} else if (position == 2) {
btn1.setImageResource(R.drawable.ic_pause_circle_outline_black_24dp);
btn2.setImageResource(R.drawable.ic_pause_circle_outline_black_24dp);
btn3.setImageResource(R.drawable.ic_play_circle_outline_black_24dp);
}
}
}