How to get to the beginning of activity with MediaPlayer

Need to pick another random track after stopping the previous one by clicking "stop" button

I'm playing a random track by clicking "play" button, stop it by clicking "stop" and then i need to randomize again, in other words, to pick another track.

protected void onCreate(Bundle savedInstanceState) {



Button play36 = (Button)findViewById(;

Button stop = (Button)findViewById(;

String[] listOfFiles = new String[0];

try {

listOfFiles = getAssets().list("");

List musicOnlyList = new ArrayList<>();

for(int i = 0; i < listOfFiles.length; i++){

if (getExtension(listOfFiles[i]).equals("mp3"))



int itemIndex = new Random().nextInt(musicOnlyList.size());

String file = musicOnlyList.get(itemIndex);

AssetFileDescriptor afd = null;

afd = getAssets().openFd(file);

final MediaPlayer mediaPlayer = new MediaPlayer();

mediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());



Read More »

By: StackOverFlow - 5 days ago

Related Posts