Я хотел бы добавить кнопку повтора (onClick) в мой медиаплеер.У меня есть кнопки воспроизведения, паузы и остановки, как в коде ниже.Как добавить кнопку «Повторить», чтобы музыка воспроизводилась в медиапроигрывателе, чтобы она повторялась как цикл с помощью кнопки «Повторить», нажимаемой пользователем.Это мой код медиаплеера:
public class Musica extends AppCompatActivity implements Runnable {
private Button pause;
private Button stop;
private SeekBar mseek;
private MediaPlayer mp;
private Thread soundThread;
private Button play;
//list
AdRequest adRequest;
private AdView adView;
ListView listm;
String[] itemname = {
"music 1",
"music 2",
"music 3",
"music 4",
"music 5",
"music 6",
"Lullaby 1",
"Lullaby 2",
"Lullaby 3",
"Lullaby 4",
"Lullaby 5",
};
Integer[] imgid = {
R.drawable.musicon,
R.drawable.musicon,
R.drawable.musicon,
R.drawable.musicon,
R.drawable.musicon,
R.drawable.musicon,
R.drawable.musicon,
R.drawable.musicon,
R.drawable.musicon,
R.drawable.musicon,
R.drawable.musicon,
};
@Override
protected void onDestroy() {
super.onDestroy();
if (mp != null) {
mp.stop();
mp.release();
mp = null;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_musica);
getSupportActionBar().hide();
adView = (AdView)findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder()
.build();
adView.loadAd(adRequest);
adView.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
adView.setVisibility(View.VISIBLE);
}
@Override
public void onAdFailedToLoad(int error) {
adView.setVisibility(View.GONE);
}
});
CustomListAdapterMusic adapter = new CustomListAdapterMusic(this, itemname, imgid);
listm = (ListView) findViewById(R.id.listmusic);
listm.setAdapter(adapter);
listm.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
String Slecteditem = itemname[+position];
Toast.makeText(getApplicationContext(), Slecteditem, Toast.LENGTH_SHORT).show();
if (position == 0) {
stopPlaying();
mp = MediaPlayer.create(Musica.this.getBaseContext(), R.raw.babyone);
mp.start();
}
if (position == 1) {
stopPlaying();
mp = MediaPlayer.create(Musica.this.getBaseContext(), R.raw.babytwo);
mp.start();
}
if (position == 2) {
stopPlaying();
mp = MediaPlayer.create(Musica.this.getBaseContext(), R.raw.water);
mp.start();
}
if (position == 3) {
stopPlaying();
mp = MediaPlayer.create(Musica.this.getBaseContext(), R.raw.ocean);
mp.start();
}
if (position == 4) {
stopPlaying();
mp = MediaPlayer.create(Musica.this.getBaseContext(), R.raw.rain);
mp.start();
}
if (position == 5) {
stopPlaying();
mp = MediaPlayer.create(Musica.this.getBaseContext(), R.raw.sm);
mp.start();
}
if (position == 6) {
stopPlaying();
mp = MediaPlayer.create(Musica.this.getBaseContext(), R.raw.classica);
mp.start();
}
if (position == 7) {
stopPlaying();
mp = MediaPlayer.create(Musica.this.getBaseContext(), R.raw.relax6);
mp.start();
}
if (position == 8) {
stopPlaying();
mp = MediaPlayer.create(Musica.this.getBaseContext(), R.raw.twinkle7);
mp.start();
}
if (position == 9) {
stopPlaying();
mp = MediaPlayer.create(Musica.this.getBaseContext(), R.raw.ninar11);
mp.start();
}
if (position == 10) {
stopPlaying();
mp = MediaPlayer.create(Musica.this.getBaseContext(), R.raw.lullaby9);
mp.start();
}
play = (Button) findViewById(R.id.bplay);
pause = (Button) findViewById(R.id.bpause);
stop = (Button) findViewById(R.id.bstop);
mseek = (SeekBar) findViewById(R.id.seekBar);
setupListeners();
soundThread = new Thread(Musica.this);
soundThread.start();
}
});
}
private void stopPlaying() {
if (mp != null) {
mp.stop();
mp.release();
mp = null;
}
}
private void setupListeners()
{
play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mp.start();
}
});
pause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mp.pause();
}
});
stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View currentView) {
mp.stop();
}
});
mseek.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (fromUser) {
mp.seekTo(progress);
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
@Override
public void run() {
int currentPosition = 0;
int soundTotal = mp.getDuration();
mseek.setMax(soundTotal);
while (mp != null && currentPosition < soundTotal) {
try {
Thread.sleep(300);
currentPosition = mp.getCurrentPosition();
} catch (InterruptedException SoundException) {
return;
} catch (Exception otherException) {
return;
}
mseek.setProgress(currentPosition);
}
}
}