Skip to content
Advertisement

Playing Multiple Mediaplayers with OnCompletionListener in Android Studio

help me, I’m a beginner, I’m trying to make an application in which there is audio that needs to be played, I try to use MediaPlayer, so that the audio can be played and paused, I have implemented these 3 mediaplayer with setOnCompletion and array methods when calling the audio folder,like this:

JavaScript

and this array for file MediaPlayer:

JavaScript

but when I clicked button, the program that I made does not work, the medialayer and button no response, what should I add, I have redesigned this code many times but the results remain the same, is there a code or method that was missed?

This is for the full source code that I have:

JavaScript

Advertisement

Answer

There are few crucial things that you missed :

  • to set onClickListner() in those Button objects
  • to reuse the MediaPlayer instance(you created new one everytime you need)
  • to properly update the play variable which I believe is the index of currently played song

I tried to make it fully readable as I can. Here is the code for MainActivity:

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement