On the site I found the code:

public void onClick (View v){ index = 0; mp = MediaPlayer.create(getApplicationContext(), sounds[index] ); mp.setLooping(false); mp.start(); mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener(){ public void onCompletion(MediaPlayer mp){ if(index < sounds.length-1){ index++; mp = MediaPlayer.create(getApplicationContext(), sounds[index]); mp.start(); mp.setOnCompletionListener(this); } else mp.release(); } }); } 

But he plays all the tracks one after the other. Can someone tell me: how do you play a single track when you click on the button? And while the track is playing, the button must be inactive, and the next press plays the second track. And how to make control from the volume + button?

    2 answers 2

    So write down this behavior. When pressed, make the button inactive, and when finished, return:

     int index = 0; //Π΄Π°Π±Ρ‹ Π½Π΅ ΡΠ±Ρ€Π°ΡΡ‹Π²Π°Π»ΠΎΡΡŒ ΠΏΠΎ Π½Π°ΠΆΠ°Ρ‚ΠΈΡŽ Π½Π° ΠΊΠ½ΠΎΠΏΠΊΡƒ выносим public void onClick (View v){ yourButton.setEnabled(false); //ΠΊΠ½ΠΎΠΏΠΊΠ° Π½Π΅Π°ΠΊΡ‚ΠΈΠ²Π½Π° mp = MediaPlayer.create(getApplicationContext(), sounds[index]); mp.setLooping(false); mp.start(); mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener(){ public void onCompletion(MediaPlayer mp){ yourButton.setEnabled(true); //послС окончания воспроизвСдСния Π΄Π΅Π»Π°Π΅ΠΌ ΠΊΠ½ΠΎΠΏΠΊΡƒ Π°ΠΊΡ‚ΠΈΠ²Π½ΠΎΠΉ mp.release(); if(index < sounds.length-1){ index++; //ΡƒΠ²Π΅Π»ΠΈΡ‡ΠΈΠ²Π°Π΅ΠΌ счСтчик для воспроизвСдСния ΡΠ»Π΅Π΄ΡƒΡŽΡ‰Π΅Π³ΠΎ Ρ‚Ρ€Π΅ΠΊΠ° } else { index = 0; //сбрасываСм Π² 0 } } }); } 
    • Yes, you are right, your answer is correct - miha_dev
    • Thanks It works!!! - Vasily
    • @ Vasily, if you were given the correct answer, tick it as correct for those who later stumble upon this topic. - Denis

    Rewrite the onCompletion(MediaPlayer mp) method code:

     public void onCompletion(MediaPlayer mp){ mp.release(); v.setEnabled(true); if(index < sounds.length-1) { index++; } } 

    To play the next track, you need to increase the index by one.