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) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

Button play36 = (Button)findViewById(R.id.threesix);

Button stop = (Button)findViewById(R.id.stop);

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"))

musicOnlyList.add(listOfFiles[i]);

}

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());

mediaPlayer.prepare();

play36.setOnClickListener(new...

Read More »

By: StackOverFlow - 5 days ago

Related Posts